POJ 2455-secret Milking Machine (Network flow _ Max Stream + binary search)

Source: Internet
Author: User

Secret Milking Machine
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10119 Accepted: 2973

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

Test instructions: There are n points in an image, P bar, each edge has a weight, and each edge can only be used once, ask to find the sum bar from 1 to n path, so that the sum of the path through the edge, the maximum weight value of the smallest. (not path length and, is the distance between two adjacent points in the path)

Idea: The longest part of the binary path, according to the two score composition.
Composition method:
If the two-point path is less than X, an edge is connected between two points, and the weight is 1 (if it is already connected, the weight is added to 1).
The maximum flow is the number of path bars that are not duplicated from 1 to N, judging if they are greater than the specified T bar.

The heavy side should be used as two sides.

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include < algorithm> #include <queue> #include <set> #include <map>using namespace Std;const int inf=    0x3f3f3f3f;int head[510],num[510],d[510],pre[510],cur[510],q[510];int n,s,t,nv,cnt,sum;int maxint=inf;struct Node {    int u,v,cap; int next;}    Edge[4000010];struct node1 {int u,v; int W;}    p[4000010];void Add (int u,int v,int cap) {edge[cnt].v=v;    Edge[cnt].cap=cap;    Edge[cnt].next=head[u];    head[u]=cnt++;    Edge[cnt].v=u;    Edge[cnt].cap=cap;    EDGE[CNT].NEXT=HEAD[V]; head[v]=cnt++;}    void BFs () {memset (num,0,sizeof (num));    Memset (d,-1,sizeof (d));    int f1=0,f2=0,i;    q[f1++]=t;    Num[0]=1;    d[t]++;        while (F1&GT;=F2) {int u=q[f2++];            for (i=head[u]; i!=-1; i=edge[i].next) {int v=edge[i].v;            if (d[v]!=-1) continue;            d[v]=d[u]+1;            num[d[v]]++;        Q[f1++]=v; }}}int ISAP () {memcpy (cur,head,sizeof (cur));    int flow=0, u=pre[s]=s, I;    BFS ();            while (D[S]&LT;NV) {if (u==t) {int f=maxint, POS;                    for (i=s; i!=t; i=edge[cur[i]].v) {if (f>edge[cur[i]].cap) {f=edge[cur[i]].cap;                Pos=i;                }} for (i=s; i!=t; i=edge[cur[i]].v) {edge[cur[i]].cap-=f;            Edge[cur[i]^1].cap+=f;            } flow+=f;            if (flow>=sum) return flow;        U=pos;                } for (i=cur[u]; i!=-1; i=edge[i].next) {if (D[edge[i].v]+1==d[u]&&edge[i].cap) {            Break            }} if (I!=-1) {cur[u]=i;            Pre[edge[i].v]=u;        U=EDGE[I].V;            } else {if (--num[d[u]]==0) break;            int MIND=NV; for (i=head[u]; i!=-1; i=edge[i].next) {if (mind>d[edge[i].v]&&edge[i].cap) {MIND=D[EDGE[I].V];                Cur[u]=i;            }} d[u]=mind+1;            num[d[u]]++;        U=pre[u]; }} return flow;}    int main () {int n, P, I, J; while (~SCANF ("%d%d%d", &n,&p,&sum)} {for (i=0;i<p;i++) scanf ("%d%d%d", &p[i].u,&amp        ;p [I].V,&AMP;P[I].W];        int low=1,mid,high=1000001;        int ans=-1, x;            while (Low<=high) {mid= (High+low)/2;            S=1;            T=n;            nv=t+1;            cnt=0;            memset (head,-1,sizeof (head));                for (i=0; i<p; i++) {if (P[i].w<=mid) {Add (p[i].u,p[i].v,1);            }} x=isap ();                if (x>=sum) {ans=mid;            High=mid-1;        } else low=mid+1;    } printf ("%d\n", ans); } return 0;}



POJ 2455-secret Milking Machine (Network flow _ Max Stream + binary search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.