Optimal Milking
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 18613 | Accepted: 6650 | |
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题意:农场主有k个产奶机,c个奶牛,每个产奶机最多可供M头奶牛使用,在奶牛和挤奶器之间有一组不同长度的路。K个挤奶器的位置用1~K的编号标明,奶牛的位置用K+1~K+C 的编号标明。寻找一个方案,安排每头奶牛到某个挤奶器挤奶,并使得C 头奶牛需要走的所有路程中的最大路程最小。每个测试数据中至少有一个安排方案。每条奶牛到挤奶器有多条路。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3;
int k,c,m;
int mapp[300][300],mp[300][300],v[300],pp[300][300];
bool used[300];
void floyd()//先找出来最短路
{
for(int i=0;i<k+c;i++)
{
for(int j=0;j<k+c;j++)
{
for(int l=0;l<k+c;l++)
{
if(mp[j][l]>mp[j][i]+mp[i][l])
mp[j][l]=mp[j][i]+mp[i][l];
}
}
}
}
int found(int x)//多重匹配
{
for(int i=0; i<k; i++)
{
if(mapp[x][i]&&!used[i])
{
used[i]=1;
if(v[i]<m)
{
pp[i][v[i]++]=x;
return 1;
}
for(int j=0; j<v[i]; j++)
{
if(found(pp[i][j]))
{
pp[i][j]=x;
return 1;
}
}
}
}
return 0;
}
int aa()
{
memset(v,0,sizeof(v));
for(int i=0; i<c; i++)
{
memset(used,0,sizeof(used));
if(!found(i)) return 0;
}
return 1;
}
int main()
{
while(~scanf("%d%d%d",&k,&c,&m))
{
for(int i=0; i<k+c; i++)
for(int j=0; j<k+c; j++)
{
scanf("%d",&mp[i][j]);
if(mp[i][j]==0)
mp[i][j]=inf;
}
floyd();
int l=0,r=100000,mid;
while(l<r)//二分查找答案
{
mid=(l+r)/2;
memset(mapp,0,sizeof(mapp));
for(int i=k; i<k+c; i++)
{
for(int j=0; j<k; j++)
{
if(mp[i][j]<=mid)
mapp[i-k][j]=1;
}
}
if(aa())
r=mid;
else
l=mid+1;
}
printf("%d\n",r);
}
}