您的位置:首页 > 其它

POJ 2455 Secret Milking Machine 最大流 二分答案

2016-04-09 10:42 591 查看
源汇为1,n的无向图求最小的路径上最大边使存在T条不重叠的路连通源汇。

显然二分答案最大边长,判定时只加入满足最大边长的边。注意是无向边。。

数据:http://contest.usaco.org/FEB05_6.htm

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

int level
, cnt, cur
, v[M], w[M], p[M], h
, q[M], s, t;
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] = c; 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 a[M], b[M], c[M];
int main() {
int i, j, mid, l, r, n, m, k, ans;
while (scanf("%d%d%d", &n, &m, &k) == 3) {
s = 1; t = n; ans = -1;
l = inf; r = 0;
FOR(i,1,m) {
scanf("%d%d%d", &a[i], &b[i], &c[i]);
l = min(l, c[i]); r = max(r, c[i]);
}
while (l <= r) {
mid = l + r >> 1; cnt = 1;
memset(h, 0, sizeof h);
FOR(i,1,m) if (c[i] <= mid) add(a[i], b[i], 1);
if (dinic() >= k) ans = mid, r = mid - 1;
else l = mid + 1;
}
printf("%d\n", ans);
}
return 0;
}


Secret Milking Machine

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 11263 Accepted: 3276

Description

Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within his farm and needs to be able to get to the machine without being detected. He must make a total of T (1 <= T <= 200) trips to the machine during its construction. He has a secret tunnel that he uses only for the return trips.

The farm comprises N (2 <= N <= 200) landmarks (numbered 1..N) connected by P (1 <= P <= 40,000) bidirectional trails (numbered 1..P) and with a positive length that does not exceed 1,000,000. Multiple trails might join a pair of landmarks.

To minimize his chances of detection, FJ knows he cannot use any trail on the farm more than once and that he should try to use the shortest trails.

Help FJ get from the barn (landmark 1) to the secret milking machine (landmark N) a total of T times. Find the minimum possible length of the longest single trail that he will have to use, subject to the constraint that he use no trail more than once. (Note well: The goal is to minimize the length of the longest trail, not the sum of the trail lengths.)

It is guaranteed that FJ can make all T trips without reusing a trail.

Input

Line 1: Three space-separated integers: N, P, and T

Lines 2..P+1: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, indicating that a trail connects landmark A_i to landmark B_i with length L_i.

Output

Line 1: A single integer that is the minimum possible length of the longest segment of Farmer John’s route.

Sample Input

7 9 2
1 2 2
2 3 5
3 7 5
1 4 1
4 3 1
4 5 7
5 7 1
1 6 3
6 7 3


Sample Output

5


Hint

Farmer John can travel trails 1 - 2 - 3 - 7 and 1 - 6 - 7. None of the trails travelled exceeds 5 units in length. It is impossible for Farmer John to travel from 1 to 7 twice without using at least one trail of length 5.

Huge input data,scanf is recommended.

Source

USACO 2005 February Gold
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: