POJ_2112_Optimal Milking(最大流+二分)

来源:互联网 发布:火车票抢购软件 编辑:程序博客网 时间:2024/06/01 19:23
Optimal Milking
Time Limit: 2000MS Memory Limit: 30000KTotal Submissions: 14149 Accepted: 5104Case Time Limit: 1000MS

Description

FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations are named by ID numbers 1..K; the cow locations are named by ID numbers K+1..K+C.

Each milking point can "process" at most M (1 <= M <= 15) cows each day.

Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine.

Input

* Line 1: A single line with three space-separated integers: K, C, and M.

* Lines 2.. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.

Output

A single line with a single integer that is the minimum possible total distance for the furthest walking cow.

Sample Input

2 3 20 3 2 1 13 0 3 2 02 3 0 1 01 2 1 0 21 0 0 2 0

Sample Output

2

题意:k个机器,每个机器最多服务m头牛。c头牛,每个牛需要1台机器来服务。告诉你牛与机器每个之间的直接距离。问:让所有的牛都被服务的情况下,使走的最远的牛的距离最短,求这个距离。

分析:二分距离,然后最大流判可行性。先用floyd求出最短路,求的过程要注意,当u==v || (u,v)没有直接相连的路径时值为0,floyd的时候要先判断。然后每次二分的值mid,建图时,把机器和牛的最短路小于等于mid的边加进去,容量为1;加入超级源点s,使得s指向所有的机器,容量为M;加入超级汇点t,使得所有的牛指向t,容量为1;然后跑出的最大流maxflow若等于牛的数量C,则说明可行,将区间右边界缩小至mid;若小于C的话就将区间左边界增大至mid(左开右闭)。最后得到的区间右边界值即为结果。

题目链接:http://poj.org/problem?id=2112

代码清单:

#include<map>#include<set>#include<cmath>#include<queue>#include<stack>#include<ctime>#include<cctype>#include<string>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespace std;#define end() return 0typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;const int maxn = 60000 + 5;const int INF = 0x7f7f7f7f;struct Edge{    int from,to,cap,flow;    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}};struct dinic{    int n,m,s,t; //结点数,边数(包括反向弧),源点,汇点    vector<Edge>edge;//边表。edge[e]和edge[e^1]互为反向弧    vector<int>G[maxn];//邻接表。G[i][j]表示结点i的第j条边在e数组的序号    bool vis[maxn]; //bfs用    int d[maxn]; //从起点到i的距离    int cur[maxn]; //当前弧下标    void init(int n,int s,int t){        this -> n = n;        this -> s = s;        this -> t = t;        for(int i=0;i<=n;i++) G[i].clear();        edge.clear();    }    void addEdge(int from,int to,int cap){        edge.push_back(Edge(from,to,cap,0));        edge.push_back(Edge(to,from,0,0));        m=edge.size();        G[from].push_back(m-2);        G[to].push_back(m-1);    }    bool bfs(){        memset(vis,false,sizeof(vis));        queue<int>q;        q.push(s);        d[s]=0;        vis[s]=true;        while(!q.empty()){            int x=q.front();q.pop();            for(int i=0;i<G[x].size();i++){                Edge& e=edge[G[x][i]];                if(!vis[e.to]&&e.cap>e.flow){ //只考虑残量网络中的弧                    vis[e.to]=true;                    d[e.to]=d[x]+1;                    q.push(e.to);                }            }        }        return vis[t];    }    int dfs(int x,int a){        if(x==t||a==0) return a;        int flow=0,f;        for(int& i=cur[x];i<G[x].size();i++){ // & -> 从上次考虑的弧            Edge& e=edge[G[x][i]];            if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0){                e.flow+=f;                edge[G[x][i]^1].flow-=f;                flow+=f;                a-=f;                if(a==0) break;            }        }        return flow;    }    int maxflow(){        int flow=0;        while(bfs()){            memset(cur,0,sizeof(cur));            flow+=dfs(s,INF);        }        return flow;    }};int K,C,M;int le,ri,mid,tail;int tmap[250][250];dinic dc;void input(){    scanf("%d%d%d",&K,&C,&M);    for(int i=1;i<=K+C;i++){        for(int j=1;j<=K+C;j++)            scanf("%d",&tmap[i][j]);    }}void floyd(){    for(int k=1;k<=K+C;k++){        for(int i=1;i<=K+C;i++){            for(int j=1;j<=K+C;j++){                if(i==j) continue;                if(tmap[i][k]&&tmap[k][j]&&(tmap[i][j]==0||tmap[i][j]>tmap[i][k]+tmap[k][j]))                    tmap[i][j]=tmap[i][k]+tmap[k][j];            }        }    }}int work(){    floyd();    tail=K+C+1;    le=0; ri=200*(K+C);    while(ri-le>1){        mid=(le+ri)>>1;        dc.init(tail+1,0,tail);        for(int i=1;i<=K;i++){            dc.addEdge(0,i,M);        }        for(int i=K+1;i<=K+C;i++){            dc.addEdge(i,tail,1);        }        for(int i=1;i<=K;i++){            for(int j=K+1;j<=K+C;j++){                if(tmap[i][j]&&tmap[i][j]<=mid)                    dc.addEdge(i,j,tmap[i][j]);            }        }        int sum=dc.maxflow();        if(sum==C) ri=mid;        else le=mid;    }return ri;}void solve(){    printf("%d\n",work());}int main(){    input();    solve();    end();}


0 0