Optimal Milking
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 9267 | Accepted: 3345 | |
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.
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.
* 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
Source
思路:先求出每个点之间的最短距离,然后二分查找答案,对于当前的mid,如果从Ki到Cj的路径不为0且长度小于mid,连接一条(i,j,1)的边,源点到Ki连接容量为M的边,Cj到汇点连接容量为1的边,如果最大流可行为C,那么更新ans;
#include<cstdio>
using namespace std;
const int mm=1000000;
const int mn=250;
const int oo=1000000000;
int node,s,t,edge;
int to[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn],floyd[mn][mn];
inline int min(int a,int b)
{
return a<b?a:b;
}
inline int max(int a,int b)
{
return a>b?a:b;
}
inline void init(int nn,int ss,int tt)
{
node=nn,s=ss,t=tt,edge=0;
for(int i=0; i<node; ++i) head[i]=-1;
}
inline void add(int u,int v,int c)
{
to[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
to[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool bfs()
{
int i,u,v,l,r=0;
for(i=0; i<node; ++i) dis[i]=-1;
dis[q[r++]=s]=0;
for(l=0; l<r; ++l)
for(i=head[u=q[l]]; i>=0; i=next[i])
if(flow[i]&&dis[v=to[i]]<0)
{
dis[q[r++]=v]=dis[u]+1;
if(v==t)return 1;
}
return 0;
}
int dfs(int u,int maxf)
{
if(u==t) return maxf;
for(int &i=work[u],v,tmp; i>=0; i=next[i])
if(flow[i]&&dis[v=to[i]]==dis[u]+1&&(tmp=dfs(v,min(maxf,flow[i])))>0)
{
flow[i]-=tmp;
flow[i^1]+=tmp;
return tmp;
}
return 0;
}
int dinic()
{
int i,ret=0,delta;
while(bfs())
{
for(i=0; i<node; ++i) work[i]=head[i];
while(delta=dfs(s,oo))ret+=delta;
}
return ret;
}
int main()
{
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",&floyd[i][j]);
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(floyd[i][k]&&floyd[k][j])
{
if(floyd[i][j])
floyd[i][j]=min(floyd[i][k]+floyd[k][j],floyd[i][j]);
else floyd[i][j]=floyd[i][k]+floyd[k][j];
}
int l=oo,r=0;
for(int i=1; i<=K; i++)
for(int j=K+1; j<=K+C; j++)
if(floyd[i][j])
l=min(floyd[i][j],l),r=max(floyd[i][j],r);
int ans;
while(r>=l)
{
int mid=(l+r)>>1;
init(K+C+2,0,K+C+1);
for(int i=1; i<=K; i++) add(s,i,M);
for(int j=K+1; j<=K+C; j++) add(j,t,1);
for(int i=1; i<=K; i++)
for(int j=K+1; j<=K+C; j++)
if(floyd[i][j]&&floyd[i][j]<=mid) add(i,j,1);
if(dinic()==C)
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
printf("%d\n",ans);
}
return 0;
}