Optimal Milking
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 4895 | Accepted: 1874 | |
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
这个题写得很郁闷,搞了两天终于AC了。。
网上对这个题目的方法已经解释的很清楚了,我就不再赘述啦。。
Source Code
Problem: 2112 | User: athenaa | |
Memory: 808K | Time: 938MS | |
Language: C++ | Result: Accepted |
- Source Code
#include<stdio.h> #include<algorithm> #include<queue> #define inf 99999999 using namespace std; int k,c,m,n,s,t; int map[300][300]; int dis[300][300]; int pre[300]; void floyd() { int x,y,z; for(z=1;z<=n;z++) for(x=1;x<=n;x++) for(y=1;y<=n;y++) { if(dis[x][z]!=inf&&dis[z][y]!=inf) { if(dis[x][y]==inf||dis[x][y]>dis[x][z]+dis[z][y]) dis[x][y]=dis[x][z]+dis[z][y]; } } } void bulid(int mid) { int i,j; memset(map,0,sizeof(map)); for(i=1;i<=k;i++) for(j=k+1;j<=n;j++) if(mid>dis[i][j]) map[i][j]=1; for(i=1;i<=k;i++) map[s][i]=m; for(i=k+1;i<=n;i++) map[i][t]=1; } bool bfs() { int i,now; memset(pre,-1,sizeof(pre)); queue<int>q; q.push(s); while(!q.empty()) { now=q.front(); q.pop(); for(i=0;i<=t;i++) { if(pre[i]==-1&&map[now][i]>0) { pre[i]=now; if(i==t) return true; q.push(i); } } } return false; } int EK() { int flow=0,min,i; while(bfs()) { min=inf; for(i=t;i!=s;i=pre[i]) if(map[pre[i]][i]<min) min=map[pre[i]][i]; for(i=t;i!=s;i=pre[i]) { map[pre[i]][i]=map[pre[i]][i]-min; map[i][pre[i]]=map[i][pre[i]]+min; } flow=flow+min; } return flow; } int main() { int i,j,x,MAX=0,tmp; int left,right,mid; scanf("%d%d%d",&k,&c,&m); s=0; n=k+c; t=n+1; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { scanf("%d",&x); if(x==0&&i!=j) dis[i][j]=inf; else dis[i][j]=x; } floyd(); for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(dis[i][j]>MAX) MAX=dis[i][j]; left=0; right=MAX; while(left<=right) { mid=(left+right)/2; bulid(mid); tmp=EK(); if(tmp==c) right=mid-1; else left=mid+1; } printf("%d/n",left-1); return 0; }