POJ 2112 Optimal Milking

 

Optimal Milking
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 3151   Accepted: 1260
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

 

 

/* 一般二分图匹配中,在一个匹配中,二分图中的 X 部与 Y 部中的点是一对一的。二分图多重匹配中,X 部中的点可以匹配多个 Y 部中的点, Y 部中的点最多只能匹配一个 X 部中的点 FLOYD求每对顶点间的最短路径 + 多重匈牙利算法 + 二分 */ #include <iostream> #define MAX_C 250 #define MAX_K 50 #define MAX_M 20 #define MAX_N 250 #define MAX_VAL 99999999 #define minv(a, b) ((a) <= (b) ? (a) : (b)) int dist[MAX_N][MAX_N][2]; int pre[MAX_K][MAX_M]; int k, c, m; bool v[MAX_K]; //初始化 void init() { int d; scanf("%d%d%d", &k, &c, &m); for(int i = 1; i <= k + c; i++) { for(int j = 1; j <= k + c; j++) { scanf("%d", &d); if(d == 0) dist[i][j][0] = MAX_VAL; else dist[i][j][0] = d; } } } //求每对顶点间的最短路径 void floyd() { int cp, lp = 0; for(int kp = 1; kp <= k + c; kp++) { cp = 1 - lp; for(int i = 1; i <= k + c; i++) { for(int j = 1; j <= k + c; j++) dist[i][j][cp] = minv(dist[i][j][lp], dist[i][kp][lp] + dist[kp][j][lp]); } lp = cp; } } //判断当前定点可不可以找到一条增广路径 bool canGo(int cid, int maxDist) { for(int i = 1; i <= k; i++) { if(dist[cid][i][(c + k) % 2] > maxDist) continue; if(v[i]) continue; v[i] = true; int num = pre[i][0]; if(num < m) { pre[i][++pre[i][0]] = cid; return true; } else if(num == m) { for(int j = 1; j <= pre[i][0]; j++) { if(canGo(pre[i][j], maxDist)) { pre[i][j] = cid; return true; } } } } return false; } //检查最大距离为maxDist是否可行 bool check(int maxDist) { memset(pre, 0, sizeof(pre)); int total = 0; for(int i = k + 1; i <= k + c; i++) { memset(v, 0, sizeof(v)); if(canGo(i, maxDist)) total++; } if(total == c) return true; else return false; } //二分 int partition() { int l = 0, r = 55000, m; while(l <= r) { m = (l + r) / 2; if(check(m)) r = m - 1; else l = m + 1; } return l; } int main() { init(); floyd(); printf("%d/n", partition()); return 0; } 

你可能感兴趣的:(c,Integer,input,each,distance,Numbers)