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 severa
l 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 severa
l paths on the way to their milking machine.
* 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.
A single line with a single integer that is the minimum possible total distance for the furthest walking cow.
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
2
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
const int INF=100000;
using namespace std;
int kk,c,m,n,mid;
int dis[1300][1300];
int cnt[1300];
int a[1300][1300];
int v[1300];
void floyd()
{
int i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
for(k=1;k<=n;k++)
dis[j][k]=min(dis[j][i]+dis[i][k],dis[j][k]);
}
int dfs(int u)
{
for(int i=1;i<=kk;i++)
{
if(v[i]) continue;
if(dis[u][i]>mid) continue;
v[i]=1;
if(cnt[i]<m)
{
a[i][cnt[i]++]=u;
return 1;
}
else
{
for(int j=0;j<cnt[i];j++)
{
if(dfs(a[i][j]))
{
a[i][j]=u;
return 1;
}
}
}
}
return 0;
}
int hh()
{
memset(cnt,0,sizeof(cnt));
for(int i=kk+1;i<=n;i++)
{
memset(v,0,sizeof(v));
if(!dfs(i)) return 0;
}
return 1;
}
int main()
{
while(~scanf("%d%d%d",&kk,&c,&m))
{
n=kk+c;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&dis[i][j]);
if(dis[i][j]==0)
dis[i][j]=INF;
}
floyd();
int l=0,r=100000;
while(l<r)
{
mid=(l+r)/2;
if(hh())
r=mid;
else
l=mid+1;
}
printf("%d\n",r);
}
}