POJ 2455 Secret Milking Machine(最大流+二分)
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.
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.
* 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.
题意:要求从1到N找到T条路径,路径上的边不能重复,要求最大的边最小,问那条边是多少
思路:二分答案。每次二分一个长度,然后求最大流看能不能走出T条边。至于建图,每两条可直达的边之间建一条双向边,容量为1,最大流就是能走出的路径数。
PS:搞个有容量上限的最大流,直接清空流就不用每次都建图了。据说DINIC在单位流量的图复杂度为O(ElogV)。
#include <cstdio> #include <cstring> #include <queue> using namespace std; const int MAXN = 210; const int MAXE = 40010 * 2; const int INF = 0x7f7f7f7f; struct Dinic { int n, m, st, ed, ecnt, maxlen; int head[MAXN]; int cur[MAXN], d[MAXN]; int to[MAXE], next[MAXE], flow[MAXE], cap[MAXE], len[MAXE]; void init(int ss, int tt) { st = ss; ed = tt; ecnt = 2; memset(head, 0, sizeof(head)); } void add_edge(int u, int v, int c, int l) { len[ecnt] = l; to[ecnt] = v; cap[ecnt] = c; flow[ecnt] = 0; next[ecnt] = head[u]; head[u] = ecnt++; len[ecnt] = l; to[ecnt] = u; cap[ecnt] = c; flow[ecnt] = 0; next[ecnt] = head[v]; head[v] = ecnt++; } bool bfs() { memset(d, 0, sizeof(d)); queue<int> que; que.push(st); d[st] = 1; while(!que.empty()) { int u = que.front(); que.pop(); for(int p = head[u]; p; p = next[p]) { if(len[p] > maxlen) continue; int v = to[p]; if(!d[v] && cap[p] > flow[p]) { d[v] = d[u] + 1; que.push(v); if(v == ed) return true; } } } return d[ed]; } int dfs(int u, int a) { if(u == ed || a == 0) return a; int outflow = 0, f; for(int &p = cur[u]; p; p = next[p]) { if(len[p] > maxlen) continue; int v = to[p]; if(d[u] + 1 == d[v] && (f = dfs(v, min(a, cap[p] - flow[p]))) > 0) { flow[p] += f; flow[p ^ 1] -= f; outflow += f; a -= f; if(a == 0) break; } } return outflow; } int Maxflow(int mlen) { int ans = 0; maxlen = mlen; while(bfs()) { for(int i = 0; i <= ed; ++i) cur[i] = head[i]; ans += dfs(st, INF); } return ans; } } G; int main() { int n, m, T, left = 0, right = 0; scanf("%d%d%d", &n, &m, &T); G.init(1, n); for(int i = 0; i < m; ++i) { int a, b, c; scanf("%d%d%d", &a, &b, &c); G.add_edge(a, b, 1, c); if(right < c) right = c; } while(left < right) { memset(G.flow, 0, sizeof(G.flow)); int mid = (left + right) >> 1; if(G.Maxflow(mid) < T) left = mid + 1; else right = mid; } printf("%d\n", right); }