poj 2112 Optimal Milking
阿新 • • 發佈:2017-07-21
fine i++ sets oss can names pos secure itself Optimal Milking
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.
* 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.
A single line with a single integer that is the minimum possible total distance for the furthest walking cow.
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 17933 | Accepted: 6415 | |
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
* 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
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
翻譯:一共K臺擠奶機,每臺最多可以給M頭牛提供服務,現在一共有C頭牛,在使得C頭牛全部能產奶的情況下最小化牛的最大路程。
思路:根據題設給定的直接距離的鄰接矩陣,先通過floyd預處理任意兩個對象之間的最短距離。之後對最大路程進行二分處理,對於每一個路程,判斷最大流是否為牛的個數C.
圖的建立:設源點s,匯點t,s向每頭牛連一條流量為1的邊,每臺機器向匯點t連一條流量為M的邊,若每頭牛ci能在最大路程內走到機器kj,則牛向機器連一條流量為1的邊。
AC代碼:
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<queue> #include<set> #include<vector> #include<cstring> #include<string> using namespace std; #define INF 0x3f3f3f3f const int K_MAX = 30+2, C_MAX = 200+2, V_MAX = K_MAX+C_MAX + 2; typedef long long ll; int V; struct edge { int to, cap, rev; edge(int to, int cap, int rev) :to(to), cap(cap), rev(rev) {} }; vector<edge>G[V_MAX]; int level[V_MAX]; int iter[V_MAX]; void add_edge(int from, int to, int cap) { G[from].push_back(edge(to, cap, G[to].size())); G[to].push_back(edge(from, 0, G[from].size() - 1)); } void bfs(int s) { memset(level, -1, sizeof(level)); queue<int>que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { edge&e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t)return f; for (int &i = iter[v]; i < G[v].size(); i++) { edge&e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; for (;;) { bfs(s); if (level[t] < 0)return flow; memset(iter, 0, sizeof(iter)); int f; while ((f = dfs(s, t, INT_MAX))>0) { flow += f; } } } int k, c, m,s,t; int d[K_MAX+C_MAX][K_MAX+C_MAX]; void floyd() { for (int K = 0; K < k + c; K++) for (int i = 0; i < k + c; i++) for (int j = 0; j < k + c; j++) d[i][j] = min(d[i][j],d[i][K]+d[K][j]); } bool C(int limit) { for (int i = 0; i < V;i++) { G[i].clear(); } for (int i = 0; i < c;i++) { add_edge(s,i,1); } for (int i = 0; i < k;i++) { add_edge(c+i,t,m); } for (int i = 0; i < c;i++) {//牛與機器連邊,i:牛 for (int j = 0; j < k;j++) {//j:機器 if (d[j][k+i] <= limit) add_edge(i, c + j, 1); } } return max_flow(s,t)==c;//如果最大流等於牛的個數 } int main() { while (scanf("%d%d%d", &k, &c, &m) != EOF) { for (int i = 0; i < k + c; i++) for (int j = 0; j < k + c; j++) { scanf("%d", &d[i][j]); if (!d[i][j])d[i][j] = INF;//!!Entities not directly connected by a path have a distance of 0 } floyd(); //0~c-1:牛 //c~c+k-1:擠奶機 s = k + c, t = s+1,V=t+1; int lb = 0, ub = 200 * (k + c); while (ub - lb > 1) { int mid = (lb+ub) >> 1; if (C(mid))ub = mid; else lb = mid; } printf("%d\n",ub); } return 0; }
poj 2112 Optimal Milking