Optimal Milking
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 10100 | Accepted: 3672 | |
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
#include <cstdio> #include <cstring> #define MAX 300 #define INF 10000000 int dis[MAX][MAX]; //任意连点间的最短路径 int map[MAX][MAX]; //容量网络 bool sign[MAX][MAX]; //层次网络 bool used[MAX]; //标志数组 int K, C, n, M; int min( int a, int b ) { return a < b ? a : b; } void Bulid_Graph( int min_max ) //构建容量网络 { int i, j; memset( map, 0, sizeof( map ) ); //初始化 for( i = K+1; i <= n; i++ ) map[0][i] = 1; for( i = 1; i <= K; i++ ) map[i][n+1] = M; for( i = K+1; i <= n; i++ ) { for( j = 1; j <= K; j++ ) { if( dis[i][j] <= min_max ) map[i][j] = 1; } } } bool BFS( ) //BFS构建层次网络 { //初始化 memset( used, 0, sizeof( used ) ); memset( sign, 0, sizeof( sign ) ); int queue[100*MAX] = {0}; queue[0] = 0; used[0] = 1; int t = 1, f = 0; while( f < t ) { for( int i = 0; i <= n+1; i++ ) { if( !used[i]&&map[queue[f]][i] ) { queue[t++] = i; used[i] = 1; sign[queue[f]][i] = 1; } } f++; } if( used[n+1] ) return true; //汇点在层次网络中 else return false; //汇点不在层次网络中 } int DFS( int v, int sum ) //DFS增广 { int i, s, t; if( v == n+1 ) return sum; s = sum; for( i = 0; i <= n+1; i++ ) { if( sign[v][i] ) { t = DFS( i, min( map[v][i], sum ) ); //递归调用 map[v][i] -= t; map[i][v] += t; sum -= t; } } return s-sum; } int main( ) { int i, j, k, L, R, mid, ans; scanf( "%d%d%d", &K, &C, &M ); n = K + C; //Floyd算法,求任意两点间的最短距离 for( i = 1; i <= n; i++ ) { for( j = 1; j <= n; j++ ) { scanf( "%d", &dis[i][j] ); if( dis[i][j] == 0 ) dis[i][j] = INF; } } for( k = 1; k <= n; k++ ) { for( i = 1; i <= n; i++ ) { if( dis[i][k] != INF ) { for( j = 1; j <= n; j++ ) dis[i][j] = min( dis[i][k]+dis[k][j], dis[i][j] ); } } } L = 0, R = 10000; while( L < R ) //二分法搜索 { mid = ( L+R )/2; ans = 0; //Dinic算法求最大流 Bulid_Graph( mid ); //构建容量网络(残余网络) while( BFS() ) ans += DFS( 0, INF ); //构建层次网络,并进行DFS增广 if( ans >= C ) R = mid; else L = mid+1; } printf( "%d ", R ); return 0; }