Optimal Milking
Time Limit: 2000MS |
|
Memory Limit: 30000K |
Total Submissions: 15422 |
|
Accepted: 5486 |
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
Source
USACO 2003 U S Open
题意:K个产奶机,C头奶牛,每个产奶机最多可供M头奶牛使用;并告诉了产奶机、奶牛之间的两两距离Dij(0<=i,j<K+C)。
问题:如何安排使得在任何一头奶牛都有自己产奶机的条件下,奶牛到产奶机的最远距离最短?最短是多少?
解:问最远距离最小是肯定是二分查找啦,二分距离,二分图多重匹配判断能否满流;
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
const int maxn=300,inf=0x3f3f3f3f,M=300,N=300;
int mp[maxn][maxn],map[maxn][maxn];
int vlink[M],link[M][N];
bool vis[M];
int k,c,w;
int path(int s)
{
for(int i=0;i<k;i++)///挤奶器集合
{
if(map[s][i] && !vis[i])
{
vis[i]=true;
if(vlink[i]<w)///机器可用上限
{
link[i][vlink[i]++]=s;
return 1;
}
for(int j=0;j<vlink[i];j++)
{
if(path(link[i][j]))
{
link[i][j]=s;
return 1;
}
}
}
}
return 0;
}
bool max_match()
{
int ans=0;
memset(vlink,0,sizeof(vlink));
for(int i=0;i<c;i++)///奶牛集合
{
memset(vis,false,sizeof(vis));
if(!path(i))
return false;
}
return true ;///所有奶牛都有一个挤奶器返回真
}
int main()
{
while(~scanf("%d%d%d",&k,&c,&w))
{
for(int i=0;i<c+k;i++)
{
for(int j=0;j<k+c;j++)
{
scanf("%d",&mp[i][j]);
if(mp[i][j]==0)///这里一定要修改,不然会影响floyd算法,
mp[i][j]=inf;
}
}
///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 l=0,r=inf,mid;
while(l<r)
{
mid=(l+r)/2;
memset(map,0,sizeof(map));
for(int i=k;i<c+k;i++)///奶牛->机器
{
for(int j=0;j<k;j++)
{
if(mp[i][j]<=mid)///二分查找最短距离
map[i-k][j]=1;
}
}
if(max_match())///二分图多重匹配,奶牛都有机器用时减小上线
r=mid;
else
l=mid+1;
}
cout<<r<<endl;
}
return 0;
}