POJ 2112 Optimal Milking 二分网络流

Optimal Milking
 Time Limit: 2000MS Memory Limit: 30000K Total Submissions:19179 Accepted: 6847 Case 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 2
0 3 2 1 1
3 0 3 2 0
2 3 0 1 0
1 2 1 0 2
1 0 0 2 0


Sample Output

2

#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;

const int MAX_D=4000+5;
const int MAX_B=4000000+5;
const int INF=1e9;

int tot;
int mm[MAX_D][MAX_D];

int iter[MAX_D];
int level[MAX_D];
struct node{
int to;
int cap;
int rev;
int next;
};
node edge[2*MAX_B];

edge[tot].to=b;
edge[tot].cap=c;
edge[tot].rev=tot+1;

edge[tot].to=a;
edge[tot].cap=0;
edge[tot].rev=tot-1;

}

void bfs(int s){
memset(level,-1,sizeof(level));
queue<int> que;
level[s]=0;
que.push(s);
while(!que.empty()){
int v=que.front();que.pop();
node &e=edge[i];
if(e.cap>0&&level[e.to]<0){
level[e.to]=level[v]+1;
que.push(e.to);
}
}
}
}

int dfs(int v,int t,int f){
if(v==t)return f;
for(int &i=iter[v];i!=0;i=edge[i].next){
node &e = edge[i];
if(e.cap>0&&level[v]<level[e.to]){
int d=dfs(e.to,t,min(f,e.cap));
if(d>0){
e.cap-=d;
edge[e.rev].cap+=d;
return d;
}
}
}

return 0;
}

int max_flow(int s,int t){
int flow=0;
while(1){
bfs(s);
if(level[t]<0)return flow;
for(int i=0;i<MAX_D;i++){
}
int f;
while((f=dfs(s,t,INF))>0){
flow+=f;
}
}
}

void print(int x){
cout<<endl;
for(int i=1;i<=x;i++){
for(int j=1;j<=x;j++){
cout<<mm[i][j]<<" ";
}
cout<<endl;
}
}

void solve(){
int k,c,m;
while(~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",&mm[i][j]);
if(mm[i][j]==0&&i!=j){
mm[i][j]=INF;
}
}
}
// print(k+c);

for(int i=1;i<=k+c;i++){
for(int j=1;j<=k+c;j++){
for(int w=1;w<=k+c;w++){
if(mm[j][w]>mm[j][i]+mm[i][w]){
mm[j][w]=mm[j][i]+mm[i][w];
}
}
}
}
//print(k+c);

int left=0,right=INF;

while(left<=right){
int mid=(left+right)>>1;
tot=1;
for(int i=1;i<=k;i++){
}
for(int i=1;i<=c;i++){
}

for(int i=1;i<=k;i++){
for(int j=1;j<=c;j++){
if(mm[i][j+k]<=mid){
}
}
}
int xxx=max_flow(1,2);
if(xxx==c){
right=mid-1;
}
else{
left=mid+1;
}

}

printf("%d\n",left);

}

}

int main()
{
solve();

return 0;
}