Secret Milking Machine
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10625 | Accepted: 3111 |
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.
题意:
题意:给你一个N个点(编号从1到N)和M条边的无向图以及每条边的权值。要求从1到N至少要有T条边不重复的路径,让你在满足这个前提下求出所有路径(当然是选出的那些边不重复的路径,不算没有选上的)上的最大边权值的 最小值。
解析:一看到最大值的最小化问题就想到用二分做,题目保证从1 到 N有 T 条无相同道路的路径,即每条边只能用一次,每个点可以多次经过。边不重复的路径数目可以用最大流求解。二分枚举最大边权值mid ,再枚举所有的边,若边的权值 <= mid, 加进网络流中,且容量为1,,然后跑最大流,看看最大流是不是>= T,符合的话取更新最大权值的最小值,取最小值。
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define maxn 220 #define maxm 2000000 #define INF 0x3f3f3f3f using namespace std; int N, M, T; struct NODE{ int u, v, w, next; }; NODE map[maxm]; int head1[maxn], cnt1; void initmap(){ cnt1 = 0; memset(head1, -1, sizeof(head1)); } void addmap(int u ,int v, int w){ map[cnt1].u = u; map[cnt1].v = v; map[cnt1].w = w; map[cnt1].next = head1[u]; head1[u] = cnt1++; } int maxs = 0; void input(){ initmap(); while(M--){ int a, b, c; scanf("%d%d%d", &a, &b, &c); maxs = max(maxs, c); addmap(a, b, c); addmap(b, a, c); } } struct node{ int u, v, cap, flow, next; }; node edge[maxm]; int head[maxn], cnt, cur[maxn]; int vis[maxn], dist[maxn]; void initedge(){ cnt = 0; memset(head, -1, sizeof(head)); } void addedge(int u, int v, int w){ edge[cnt].u = u; edge[cnt].v = v; edge[cnt].cap = w; edge[cnt].flow = 0; edge[cnt].next = head[u]; head[u] = cnt++; edge[cnt].u = v; edge[cnt].v = u; edge[cnt].cap = 0; edge[cnt].flow = 0; edge[cnt].next = head[v]; head[v] = cnt++; } void getmap(int ans){ for(int i = 0; i < cnt1; ++i) if(map[i].w <= ans) addedge(map[i].u, map[i].v, 1); } bool BFS(int st ,int ed){ queue<int>q; memset(vis, 0 ,sizeof(vis)); memset(dist, -1, sizeof(dist)); vis[st] = 1; dist[st] = 0; q.push(st); while(!q.empty()){ int u = q.front(); q.pop(); for(int i = head[u]; i != -1; i = edge[i].next){ node E = edge[i]; if(!vis[E.v] && E.cap > E.flow){ vis[E.v] = 1; dist[E.v] = dist[u] + 1; if(E.v == ed) return true; q.push(E.v); } } } return false; } int DFS(int x, int ed, int a){ if(x == ed || a == 0) return a; int flow = 0, f; for(int &i = cur[x]; i != -1; i = edge[i].next){ node &E = edge[i]; if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){ E.flow += f; edge[i ^ 1].flow -= f; a -= f; flow += f; if(a == 0) break; } } return flow; } int maxflow(int st, int ed){ int flowsum = 0; while(BFS(st,ed)){ memcpy(cur, head, sizeof(head)); flowsum += DFS(st, ed, INF); } return flowsum; } int main (){ while(scanf("%d%d%d", &N, &M, &T) != EOF){ input(); int l = 0, r = maxs, mid; int max_min = maxs; while(r > l){ mid = (l + r) / 2; initedge(); getmap(mid); if(maxflow(1, N) >= T){ max_min = min(max_min, mid); r = mid; } else l = mid + 1; } printf("%d\n", max_min); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。