2455 Secret Milking Machine //二分答案+SAP

来源:互联网 发布:证券公司软件哪家好 编辑:程序博客网 时间:2024/06/06 08:58

Secret Milking Machine
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3536 Accepted: 1069

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 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.

Source

USACO 2005 February Gold

 

这道题的陷阱还是不少的

首先是二分答案,注意二分答案的格式,最后输出的答案是L,而不是MID,注意!!!

再者,注意重边啊!!!!因为题目大意是说只能走一次一条边,却没有说只能一次走一个点,所以的话,就是把存在的边二分答案的时候都放进数组中,进行SAP,即可得到答案。

WA了很多次,我还是太弱了。

 

 

#include<stdio.h>

#include<string.h>

#include<vector>

using namespace std;

const int V=210,E=400100;

 

struct NODE

{

    int u,v,w;

    NODE(int uu,int vv,int ww):u(uu),v(vv),w(ww) {}

};

vector<NODE> q;

 

struct edge

{

    int aim,cap;

    edge *next,*pair;

    edge() {}

    edge(int t,int c,edge *n):aim(t),cap(c),next(n) {}

    void* operator new(unsigned,void* p)

    {

        return p;

    }

}*e[V],Te[E+E],*Pe=Te;

int num[V],dis[V];

int n,p,tt,s,t;

 

int augment(int u,int augc)

{

    if(u==t)return augc;

    int d,tmp=n-1;

    for(edge *i=e[u]; i; i=i->next)if(i->cap)

        {

            if(dis[u]==dis[i->aim]+1)

            {

                d=augment(i->aim,augc<i->cap?augc:i->cap);

                i->cap-=d,i->pair->cap+=d;

                if(d||dis[s]==n)return d;

            }

            if(dis[i->aim]<tmp)tmp=dis[i->aim];

        }

    if(!--num[dis[u]])dis[s]=n;

    num[dis[u]=tmp+1]++;

    return 0;

}

 

void init(int f)

{

    int u,v,w;

    memset(e,0,sizeof(e));

    for(int i=0;i<q.size();i++)

    {

        NODE x=q[i];

        u=x.u,  v=x.v,  w=x.w;

        if(w<=f)

        {

            e[u]=new(Pe++)edge(v,1,e[u]);

            e[v]=new(Pe++)edge(u,1,e[v]);

            e[u]->pair=e[v];

            e[v]->pair=e[u];

        }

    }

}

 

bool isap()

{

    memset(dis,0,sizeof(dis));

    memset(num,0,sizeof(num)),num[0]=n;

    int flow=0;

    s=1,t=n;

    while(dis[s]<n)  flow+=augment(s,0xfffffff);

    if(flow>=tt) return true;

    return false;

}

 

int main()

{

    while(scanf("%d%d%d",&n,&p,&tt)!=EOF)

    {

        q.clear();

        int front=10000000,top=-10000000;

        for(int i=1;i<=p;i++)

        {

            int x,y,c;

            scanf("%d%d%d",&x,&y,&c);

            q.push_back(NODE(x,y,c));

            if(c>top) top=c;

            if(c<front) front=c;

        }

        while(front<=top)

        {

            int mid=(front+top)/2;

            init(mid);

            if(isap()) top=mid-1;

            else front=mid+1;

        }

        printf("%d/n",front);

    }

    return 0;

}

原创粉丝点击