【多重二分匹配+网络流】:poj2112,Optimal Milking

题意:

K个挤奶器(编号1~K),每个每天最多挤M头奶牛。共有C头奶牛(编号K+1~K+C)。挤奶器和奶牛间有不同长度的路。

编写程序,寻找一个方案,安排每头牛到某个挤奶器挤奶,并使得C头奶牛需要走的所有路程的最大路程最小。

分析:

因为有多个挤奶器和多头奶牛,因而要求最短路需要Floyd。然后用最大流算法求解;搜索最大距离的最小值采用二分枚举。

构造网络流的方法:

1)构造一个超级源,一个超级汇

2)超级源连接所有的milking machine,其最大流量一律为M,超级汇连接所有的 奶牛其最大流量一律为1,机器与奶牛之间的流量(若两点间最短路小于或等于X,则两点就流量为1)。


注意:一同学让我帮他看代码,才意识到有一个很容易出错的小地方,千万不要用库函数定义的INT_MAX,而是要自己定义一个INTMAX=100000,否则floyd求最短路径时越界,不知道那些没AC的小盆友有没有明白~~~




/*
 * Dinic algo for max flow
 *
 * This implementation assumes that #nodes, #edges, and capacity on each edge <= INT_MAX,
 * which means INT_MAX is the best approximation of INF on edge capacity.
 * The total amount of max flow computed can be up to LLONG_MAX (not defined in this file),
 * but each 'dfs' call in 'dinic' can return <= INT_MAX flow value.
 */
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include <queue>
#include <vector>

# include<iostream>
# include<cstring>
# include<map>

#define N (250)       //==================make sure this is the total node number!
#define M (N*N+4*N)

typedef long long LL;

using namespace std;

struct edge
{
    int v, cap, next;
};
edge e[M];

int head[N], level[N], cur[N];
int num_of_edges;

//When there are multiple test sets, you need to re-initialize before each
void dinic_init(void)
{
    num_of_edges = 0;
    memset(head, -1, sizeof(head));
    return;
}

int add_edge(int u, int v, int c1, int c2)
{
    int& i=num_of_edges;

    assert(c1>=0 && c2>=0 && c1+c2>=0); // check for possibility of overflow
    e[i].v = v;
    e[i].cap = c1;
    e[i].next = head[u];
    head[u] = i++;

    e[i].v = u;
    e[i].cap = c2;
    e[i].next = head[v];
    head[v] = i++;
    return i;
}

void print_graph(int n)
{
    for (int u=0; u<n; u++)
    {
        printf("%d: ", u);
        for (int i=head[u]; i>=0; i=e[i].next)
        {
            printf("%d(%d)", e[i].v, e[i].cap);
        }
        printf("\n");
    }
    return;
}

//Find all augmentation paths in the current level graph This is the recursive version
int dfs(int u, int t, int bn)
{
    if (u == t) return bn;
    int left = bn;
    for (int i=head[u]; i>=0; i=e[i].next)
    {
        int v = e[i].v;
        int c = e[i].cap;
        if (c > 0 && level[u]+1 == level[v])
        {
            int flow = dfs(v, t, min(left, c));
            if (flow > 0)
            {
                e[i].cap -= flow;
                e[i^1].cap += flow;
                cur[u] = v;
                left -= flow;
                if (!left) break;
            }
        }
    }
    if (left > 0) level[u] = 0;
    return bn - left;
}

bool bfs(int s, int t)
{
    memset(level, 0, sizeof(level));
    level[s] = 1;
    queue<int> q;
    q.push(s);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        if (u == t) return true;
        for (int i=head[u]; i>=0; i=e[i].next)
        {
            int v = e[i].v;
            if (!level[v] && e[i].cap > 0)
            {
                level[v] = level[u]+1;
                q.push(v);
            }
        }
    }
    return false;
}

LL dinic(int s, int t)
{
    LL max_flow = 0;

    while (bfs(s, t))
    {
        memcpy(cur, head, sizeof(head));
        max_flow += dfs(s, t, INT_MAX);
    }
    return max_flow;
}

int upstream(int s, int n)
{
    int cnt = 0;
    vector<bool> visited(n);
    queue<int> q;
    visited[s] = true;
    q.push(s);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i=head[u]; i>=0; i=e[i].next)
        {
            int v = e[i].v;
            if (e[i].cap > 0 && !visited[v])
            {
                visited[v] = true;
                q.push(v);
                cnt++;
            }
        }
    }
    return cnt; // excluding s
}

# define INTMAX 10000000
int a[N][N];

int main()
{
    int k,c,m,cc,ma,l,r,mid,i,j,t;

    //input
    while(cin>>k>>c>>m)
    {
        cc=k+c;
        for(i=1;i<=cc;i++)
        {
            for(j=1;j<=cc;j++)
            {
                cin>>a[i][j];
                if(a[i][j]==0)
                {
                    a[i][j]=INTMAX;
                }
            }
        }

        //find max(min(path))
        for(t=1;t<=cc;t++)
        {
            for(i=1;i<=cc;i++)
            {
                for(j=1;j<=cc;j++)
                {
                    if(a[i][j]>a[i][t]+a[t][j])
                    {
                        a[i][j]=a[i][t]+a[t][j];
                    }
                }
            }
        }
        ma=0;
        for(i=1;i<=cc;i++)
        {
            for(j=1;j<=cc;j++)
            {
                if(a[i][j]>ma)
                {
                    ma=a[i][j];
                }
            }
        }

        //bipartition search and network flow
        l=0,r=ma;
        do
        {
            mid=(l+r)/2;
            dinic_init();
            for(i=1;i<=k;i++)
            {
                add_edge(0,i,m,0);
            }
            for(i=k+1;i<=cc;i++)
            {
                add_edge(i,cc+1,1,0);
            }
            for(i=1;i<=k;i++)
            {
                for(j=k+1;j<=cc;j++)
                {
                    if(a[i][j]<=mid)
                    {
                        //add_edge(i,j,a[i][j],0);
                        add_edge(i,j,1,0);
                    }
                }
            }
            i=dinic(0,cc+1);
            //cout<<i<<" "<<l<<" "<<mid<<" "<<r<<endl;
            if(i==c)
            {
                t=mid;
                r=mid-1;
            }
            else
            {
                l=mid+1;
            }
        }while(l<=r);

        cout<<t<<endl;
    }

    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值