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.
题目大意:
有k个机器,每个机器最多服务m头牛。有c头牛,每个牛需要1台机器来服务。牛牛之间,机器与机器之间,牛与机器之间有一些道路相连。问:让所有的牛都被服务的情况下,使走的最远的牛的距离最短,求这个距离。
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
USACO 2003 U S Open
题解
二分+网络流判定。
这道题的问法就显然想到二分。至于要每个牛都能被服务,就可以用流来跑。
我还是太弱了,wa了好几次。本以为是网络流错了,结果是floyd打错了。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 1000000
using namespace std;
int K,C,m,sum,tot,map[250][250];
int T,zz,head[250];
struct bian{int to,nx,v;} e[90002];
int q[250],h[250];
void floyd()
{
int i,j,k;
for(k=1;k<=tot;k++)
for(i=1;i<=tot;i++)
for(j=1;j<=tot;j++)
map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
/*for(i=1;i<=tot;i++)
{for(j=1;j<=tot;j++)
printf("%d ",map[i][j]);
printf("\n");
}*/
}
void init()
{
scanf("%d%d%d",&K,&C,&m);
int i,j;
tot=K+C; T=K+C+1;
for(i=1;i<=tot;i++)
for(j=1;j<=tot;j++)
{scanf("%d",&map[i][j]);
if(i<j) sum+=map[i][j];
if(map[i][j]==0) map[i][j]=inf;
}
floyd();
}
void insert(int x,int y,int z)
{
zz++; e[zz].to=y; e[zz].v=z; e[zz].nx=head[x]; head[x]=zz;
zz++; e[zz].to=x; e[zz].v=0; e[zz].nx=head[y]; head[y]=zz;
}
void build(int x)
{
int i,j;
zz=1;
memset(head,0,sizeof(head));
for(i=1;i<=K;i++) insert(i,T,m);
for(i=K+1;i<=tot;i++) insert(0,i,1);
for(i=K+1;i<=tot;i++)
for(j=1;j<=K;j++)
{if(map[i][j]<=x) insert(i,j,1);}
}
bool bfs()
{
int t=0,w=1,i,p,x;
memset(h,-1,sizeof(h));
q[t]=0; h[0]=0;
while(t<w)
{x=q[t]; t++;
for(i=head[x];i;i=e[i].nx)
{p=e[i].to;
if(h[p]==-1&&e[i].v)
{h[p]=h[x]+1; q[w]=p; w++;}
}
}
if(h[T]==-1) return false;
return true;
}
int dfs(int x,int f)
{
if(x==T) return f;
int usd=0,rest,i,p;
for(i=head[x];i;i=e[i].nx)
{p=e[i].to;
if(h[p]==h[x]+1&&e[i].v)
{rest=f-usd;
rest=dfs(p,min(rest,e[i].v));
e[i].v-=rest;
e[i^1].v+=rest;
usd+=rest;
if(usd==f) return f;
}
}
if(!usd) h[x]=-1;
return usd;
}
bool dinic(int x)
{
build(x);
int ans=0;
while(bfs()) ans+=dfs(0,0x7fffffff);
if(ans==C) return true;
else return false;
}
void work()
{
int l=1,r=20000,mid,ans;
while(l<=r)
{mid=(l+r)>>1;
if(dinic(mid)) {ans=mid; r=mid-1;}
else l=mid+1;
}
printf("%d\n",ans);
}
int main()
{
init(); work();
return 0;
}