Description
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
* 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
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
//
所有点之间的最短路径都做成一条容量为1边就行,然后增加源和汇,源与所有机器连上容量为m的边,牛与汇连上容量为1的边,然后二分答案,大于答案的边都删掉,判断是否可行即可。。。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=(1<<28);//不要开到(1<<30)
const int point_num=300;
int cap[point_num][point_num],dist[point_num],gap[point_num];//初始化见main里面
int s0,t0,n;//源,汇和点数
int find_path(int p,int limit=0x3f3f3f3f)
{
if(p==t0) return limit;
for(int i=0;i<n;i++)
if(dist[p]==dist[i]+1 && cap[p][i]>0)
{
int t=find_path(i,min(cap[p][i],limit));
if(t<0) return t;
if(t>0)
{
cap[p][i]-=t;
cap[i][p]+=t;
return t;
}
}
int label=n;
for(int i=0;i<n;i++) if(cap[p][i]>0) label=min(label,dist[i]+1);
if(--gap[dist[p]]==0 || dist[s0]>=n ) return -1;
++gap[dist[p]=label];
return 0;
}
int sap()
{
//初始化s,t
s0=0,t0=n-1;
int t=0,maxflow=0;
gap[0]=n;
while((t=find_path(s0))>=0) maxflow+=t;
return maxflow;
}
int d[point_num][point_num];
int main()
{
int k,c,m;
while(scanf("%d%d%d",&k,&c,&m)==3)
{
n=k+c;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&d[i][j]);
if(d[i][j]==0) d[i][j]=inf;
}d[i][i]=0;
}
for(int t=1;t<=n;t++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(d[i][j]>d[i][t]+d[t][j])
d[i][j]=d[i][t]+d[t][j];
}
}
}
int l=0,r=inf;
while(l<r)
{
int mid=(l+r)>>1;
//初始化
memset(cap,0,sizeof(cap));
memset(dist,0,sizeof(dist));
memset(gap,0,sizeof(gap));
//初始化cap
for(int i=k+1;i<=n;i++) cap[0][i]=1;
for(int i=1;i<=k;i++) cap[i][n+1]=m;
for(int i=k+1;i<=n;i++)
{
for(int j=1;j<=k;j++)
{
if(d[i][j]<=mid) cap[i][j]=1;
}
}
n+=2;
int ans=sap();
n-=2;
if(ans==c) r=mid;
else l=mid+1;
}
printf("%d\n",r);
}
return 0;
}