Poj 2455 Secret Milking Machine【二分+最大流】

来源:互联网 发布:免费linux主机 编辑:程序博客网 时间:2024/05/07 03:47

Secret Milking Machine

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 11570

 

Accepted: 3354

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.

Source

USACO 2005 February Gold

 

题目大意:

给你n个点,m条无向边,让你找一个值,找到t种走法:使得能够从起点1走到终点n没有重复路径的情况,并且这t种走法中每两点之间路径值不超过你要找到的这个值。


思路:


1、首先,我们想要找这样一个值最暴力最简单的方式就是枚举,然而因为枚举一定超时,而且考虑到找到的种类数会随着枚举出来的值增加而增加,满足单调递增性,那么我们二分查找ans,设定当前枚举到的值是mid;


2、根据当前值mid建图:

①设定源点S,连入节点1,设定权值为INF,表示期望获得无数条可行路径。

②设定汇点T,将n连入汇点,设定权值为INF,表示期望获得无数条可行路径。

③将m条无向边进行判断,其两点间距离如果小于等于mid,那么建立当前边(注意是无向),设定权值为1,表示这条边只能走一次(因为找到的每一种路径都不能有重复边)。


3、然后不断在二分过程中跑最大流,跑得一次最大流,如果其maxflow>=t(注意不是正好要t种路径,而是找到t种就可以,所以是大于等于),记录ans=mid,一直二分下去,直到不能二分为止。


4、直接枚举上界为1000000会TLE,需要枚举上界为最大边权值才能过。


Ac代码:


#include<stdio.h>#include<string.h>#include<queue>#include<iostream>using namespace std;#define INF 0x3f3f3f3fstruct node{    int x,y,w;}a[1000000];struct node2{    int from;    int to;    int next;    int w;}e[1000000];int head[20000];int cur[20000];int divv[20000];int n,m,t,ss,tt,cont;void add(int from,int to,int w){    e[cont].to=to;    e[cont].w=w;    e[cont].next=head[from];    head[from]=cont++;}void getmap(int mid){    ss=n+1;    tt=ss+1;    cont=0;    memset(head,-1,sizeof(head));    add(ss,1,0x3f3f3f3f);    add(1,ss,0);    add(n,tt,0x3f3f3f3f);    add(tt,n,0);    for(int i=0;i<m;i++)    {        if(a[i].w<=mid)        {            add(a[i].x,a[i].y,1);            add(a[i].y,a[i].x,0);            add(a[i].y,a[i].x,1);            add(a[i].x,a[i].y,0);        }    }}int makedivv(){    queue<int >s;    s.push(ss);    memset(divv,0,sizeof(divv));    divv[ss]=1;    while(!s.empty())    {        int u=s.front();        s.pop();        if(u==tt)return 1;        for(int i=head[u];i!=-1;i=e[i].next)        {            int v=e[i].to;            int w=e[i].w;            if(w&&divv[v]==0)            {                divv[v]=divv[u]+1;                s.push(v);            }        }    }    return 0;}int Dfs(int u,int maxflow,int tt){    if(u==tt)return maxflow;    int ret=0;    for(int &i=cur[u];i!=-1;i=e[i].next)    {        int v=e[i].to;        int w=e[i].w;        if(w&&divv[v]==divv[u]+1)        {            int f=Dfs(v,min(maxflow-ret,w),tt);            e[i].w-=f;            e[i^1].w+=f;            ret+=f;            if(ret==maxflow)return ret;        }    }    return ret;}int Dinic(int mid){    getmap(mid);    int ans=0;    while(makedivv()==1)    {        memcpy(cur,head,sizeof(head));        ans+=Dfs(ss,INF,tt);    }    return ans;}int main(){    while(~scanf("%d%d%d",&n,&m,&t))    {        int maxn=-1;        for(int i=0;i<m;i++)        {            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);            maxn=max(maxn,a[i].w);        }        int l=0;        int r=maxn;        int mid;        int ans=INF;        while(r>=l)        {            mid=(l+r)/2;            int tmp=Dinic(mid);            if(tmp<t)            {                l=mid+1;            }            else            {                ans=mid;                r=mid-1;            }        }        printf("%d\n",ans);    }}




0 0
原创粉丝点击