您的位置:首页 > 运维架构

POJ 2112 Optimal Milking 最大流 二分答案

2016-04-09 10:09 399 查看
有K个能挤M头奶牛的挤奶机和C头奶牛,告诉一些挤奶机和奶牛间距离,求最优分配方案使最大距离最小。

显然是二分答案。接下来考虑如何判定。

为了限制挤奶机发给m头牛,以及一头牛只能分配给1台挤奶机,源点连向挤奶机,容量m,牛连向汇点,容量1,距离小于二分的最大距离的 挤奶机-牛 对,连边,容量1(其实设成多大都无所谓的),跑出来最大流=c(满流?)说明方案可行。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define FOR(i,j,k) for(i=j;i<=k;++i)
using namespace std;
const int inf = 0x3f3f3f, N = 235, M = 15000;

int level
, cnt, cur
, v[M], w[M], p[M], h
, q[M], s, t, a

;
void add(int a, int b, int c) {
p[++cnt] = h[a]; v[cnt] = b; w[cnt] = c; h[a] = cnt;
p[++cnt] = h[b]; v[cnt] = a; w[cnt] = 0; h[b] = cnt;
}

bool bfs() {
int f = 0, r = 0, u, i;
memset(level, -1, sizeof level);
q[r++] = s; level[s] = 1;
while (f < r) {
u = q[f++];
for (i = h[u]; i; i = p[i]) {
if (w[i] && level[v[i]] == -1) {
level[v[i]] = level[u] + 1;
q[r++] = v[i];
}
}
}
return level[t] != -1;
}

int dfs(int u, int low) {
int i, tmp = 0, res = 0;
if (u == t) return low;
for (i = cur[u]; i && res < low; i = p[i]) {
if (w[i] && level[v[i]] == level[u] + 1) {
tmp = dfs(v[i], min(w[i], low - res));
w[i] -= tmp; w[i ^ 1] += tmp; res += tmp;
if (w[i]) cur[u] = i;
}
}
if (!res) level[u] = -1;
return res;
}

int dinic() {
int ans = 0, i;
while (bfs()) {
for (i = s; i <= t; ++i) cur[i] = h[i];
ans += dfs(s, inf);
}
return ans;
}

int main() {
int i, j, p, c, mid, l, r, n, m, k, ans;
while (scanf("%d%d%d", &k, &c, &m) == 3) {
n = k + c; s = 0; t = n + 1;
FOR(i,1,n) FOR(j,1,n) {
scanf("%d", &a[i][j]);
if (!a[i][j]) a[i][j] = inf;
}
FOR(p,1,n) FOR(i,1,n) FOR(j,1,n)
a[i][j] = min(a[i][j], a[i][p] + a[p][j]);
l = 1; r = 200 * n;
while (l <= r) {
mid = l + r >> 1; cnt = 1;
memset(h, 0, sizeof h);
FOR(i,1,k) add(s, i, m);
FOR(i,1,c) add(i + k, t, 1);
FOR(i,1,k) FOR(j,k+1,n)
if (a[i][j] <= mid)
add(i, j, 1);
if (dinic() == c) ans = mid, r = mid - 1;
else l = mid + 1;
}
printf("%d\n", ans);
}
return 0;
}


Optimal Milking

Time Limit: 2000MS Memory Limit: 30000K

Total Submissions: 15303 Accepted: 5443

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: