Optimal milking
| Time limit:2000 ms |
|
Memory limit:30000 K |
| Total submissions:12482 |
|
Accepted:4508 |
| Case time limit:1000 ms |
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 mathematical 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 20 3 2 1 13 0 3 2 02 3 0 1 01 2 1 0 21 0 0 2 0
Sample output
2
Source
Usaco 2003 u s open question: there are K milk runners, each of which can accommodate a maximum of M cows. The farm has a total of C cows, given the direct distance between the K machines and the C-way cows, we can find the minimum maximum distance from the cows to the dump when all the cows reach the dump and the conditions are met. Question: composition: first use Floyd to find the shortest distance between each other, then set the distance from the source point to each ox to 1, and the distance from each machine to the sink point to M, then, if the distance from the ox to the machine is not greater than maxdist, add the edge to the new graph, find the maximum stream for the new graph, and determine whether the maximum stream is equal to C, in this way, the maxdist is enumerated in two parts until the smallest maxdist is found.
#include <stdio.h>#include <string.h>#define inf 0x3fffffff#define maxn 235int dist[maxn][maxn], k, c, m, n;int G[maxn][maxn], Layer[maxn];int queue[maxn], maxDist;bool vis[maxn];void Floyd() { int x, i, j; maxDist = 200; for(x = 1; x <= n; ++x) for(i = 1; i <= n; ++i) for(j = 1; j <= n; ++j) if(dist[i][j] > dist[i][x] + dist[x][j]) { dist[i][j] = dist[i][x] + dist[x][j]; if(maxDist < dist[i][j]) maxDist = dist[i][j]; }}void build(int flow) { memset(G, 0, sizeof(G)); int i, j; for(i = k + 1; i <= n; ++i) { G[0][i] = 1; for(j = 1; j <= k; ++j) if(dist[i][j] <= flow) G[i][j] = 1; } for(j = 1; j <= k; ++j) G[j][n + 1] = m;}bool countLayer() { int id = 0, front = 0, now, i; memset(Layer, 0, sizeof(Layer)); Layer[0] = 1; queue[id++] = 0; while(front < id) { now = queue[front++]; for(i = 0; i <= n + 1; ++i) if(G[now][i] && !Layer[i]) { Layer[i] = Layer[now] + 1; if(i == n + 1) return true; else queue[id++] = i; } } return false;}bool Dinic() { int i, maxFlow = 0, id = 0, now, minCut, pos, u, v; while(countLayer()) { memset(vis, 0, sizeof(vis)); vis[0] = 1; queue[id++] = 0; while(id) { now = queue[id - 1]; if(now == n + 1) { minCut = inf; for(i = 1; i < id; ++i) { u = queue[i - 1]; v = queue[i]; if(G[u][v] < minCut) { minCut = G[u][v]; pos = u; } } maxFlow += minCut; for(i = 1; i < id; ++i) { u = queue[i - 1]; v = queue[i]; G[u][v] -= minCut; G[v][u] += minCut; } while(id && queue[id - 1] != pos) vis[queue[--id]] = 0; } else { for(i = 0; i <= n + 1; ++i) { if(G[now][i] && !vis[i] && Layer[now] + 1 == Layer[i]) { queue[id++] = i; vis[i] = 1; break; } } if(i > n + 1) --id; } } } return maxFlow == c;}int binarySolve() { int left = 0, right = maxDist, mid; while(left < right) { mid = (left + right) >> 1; build(mid); if(Dinic()) right = mid; else left = mid + 1; } return left;}int main() { //freopen("stdin.txt", "r", stdin); int i, j; while(scanf("%d%d%d", &k, &c, &m) == 3) { for(i = 1, n = k + c; i <= n; ++i) for(j = 1; j <= n; ++j) { scanf("%d", &dist[i][j]); if(!dist[i][j] && i != j) dist[i][j] = inf; } Floyd(); printf("%d\n", binarySolve()); } return 0;}
Poj2112 optimal milking [maximum stream + two points]