Secret Milking Machine
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 10625 |
|
Accepted: 3111 |
Description
Farmer John is constructing a new milking, and wishes to keep it secret as long as possible. He has hidden in it deep within his farm and needs to is able to get to the machine without being detected. He must make a total of T (1 <= t <=) 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 <=) landmarks (numbered 1..N) connected by P (1 <= P <= 40,000) bidirectional Trails (numbered 1..P) and with a positive length this does not exceed 1,000,000. Multiple trails might join a pair of landmarks.
To minimize him chances of detection, FJ knows he cannot use any trail in the farm more than once and that he should try T o 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'll have to use, subject to the constraint that he u Se No trail more than once. (Note well:the goal is to minimize the length of the longest trail and not the sum of the trail lengths.)
It is guaranteed this 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 land Mark A_i to landmark b_i with length l_i.
Output
* Line 1: A single integer which is the minimum possible length of the longest segment of the Farmer John ' s route.
Sample Input
7 9 21 2 22 3 53 7 51 4 14 3 14 5 75 7 11 6 36 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.
Test instructions
Test instructions: gives you an n-point (numbered from 1 to N) and M-bars without a graph and weights for each edge. Requires at least a path from 1 to n that does not duplicate the T-bar, allowing you to find the minimum value of the maximum edge value on all paths (of course, the chosen ones, not the chosen ones), in order to satisfy this premise.
Resolution: One can see the minimum of the maximum value of the problem with two points to do, the topic guaranteed from 1 to N has a T-bar path, that is, each side can only be used once, each point may be repeated. The number of paths that do not duplicate edges can be solved with the maximum flow. Two-minute enumeration of the maximum edge weights mid, and then enumerate all the edges, if the weight of the edge <= mid, added to the network stream, and the capacity of 1, and then run the maximum flow, to see if the maximum flow is not >= T, in line with the update maximum weight value of the minimum value, take the minimum value.
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAXN 220# Define MAXM 2000000#define INF 0x3f3f3f3fusing 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2455--secret Milking Machine "Two-point enumeration && maximum Flow && classic"