码迷,mamicode.com
首页 > 系统相关 > 详细

POJ2455Secret Milking Machine[最大流 无向图 二分答案]

时间:2016-11-25 22:58:16      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:pre   connect   处理   sub   技术分享   without   data   为什么   recommend   

Secret Milking Machine
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11865   Accepted: 3445

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


技术分享

奇怪的问题,bfs处理e[i].w<=mid就不对,每次重新建图就对了,不知道为什么

无向图的处理,反向边的容量也是c

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=205,INF=1e9;
int read(){
    char c=getchar();int x=0,f=1;
    while(c<0||c>9){if(c==-)f=-1; c=getchar();}
    while(c>=0&&c<=9){x=x*10+c-0; c=getchar();}
    return x*f;
}
int n,m,T,u,v,w,s,t;
struct edge{
    int v,ne,w,c,f;
}e[N*N<<1];
struct data{
    int u,v,w;
}a[N*N];
int h[N],cnt=0;
inline void ins(int u,int v,int w,int c){//printf("ins %d %d %d\n",u,v,w);
    cnt++;
    e[cnt].v=v;e[cnt].c=c;e[cnt].f=0;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
    cnt++;
    e[cnt].v=u;e[cnt].c=c;e[cnt].f=0;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}

int cur[N];
int q[N],head,tail,vis[N],d[N];

void build(int mid){
    cnt=0;
    memset(h,0,sizeof(h));
    for(int i=1;i<=m;i++) if(a[i].w<=mid) ins(a[i].u,a[i].v,a[i].w,1);
}
bool bfs(int mid){
    memset(vis,0,sizeof(vis));
    memset(d,0,sizeof(d));
    head=tail=1;
    q[tail++]=s;d[s]=0;vis[s]=1;
    while(head!=tail){
        int u=q[head++];
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v;
            if(!vis[v]&&e[i].f<e[i].c){
                q[tail++]=v;vis[v]=1;
                d[v]=d[u]+1;
                if(v==t) return 1;
            }
        }    
    }
    return 0;
}
int dfs(int u,int a){//printf("dfs %d %d\n",u,a);
    if(u==t||a==0) return a;
    int flow=0,f;
    for(int &i=cur[u];i;i=e[i].ne){
        int v=e[i].v;
        if(d[v]==d[u]+1&&(f=dfs(v,min(a,e[i].c-e[i].f)))>0){
            flow+=f;
            e[i].f+=f;
            e[((i-1)^1)+1].f-=f;
            a-=f;
            if(a==0) break;
        }
    }
    return flow;
}
int dinic(int mid){
    int flow=0;
    while(bfs(mid)){
        for(int i=s;i<=t;i++) cur[i]=h[i];
        flow+=dfs(s,INF);
    }
    //printf("flow %d\n",flow);
    return flow;
}

int main(){
    n=read();m=read();T=read();s=1;t=n;
    int l=INF,r=0,ans=INF;
    for(int i=1;i<=m;i++){
        u=read();v=read();w=read(); r=max(r,w);l=min(l,w);
        //ins(u,v,w,1);
        a[i].u=u;a[i].v=v;a[i].w=w;
    }
    while(l<=r){
        int mid=(l+r)>>1;//printf("hi %d %d %d\n",l,r,mid);
        build(mid);
        if(dinic(mid)>=T) ans=min(ans,mid),r=mid-1;
        else l=mid+1;
    }
    printf("%d",ans);
}

 

 
 

POJ2455Secret Milking Machine[最大流 无向图 二分答案]

标签:pre   connect   处理   sub   技术分享   without   data   为什么   recommend   

原文地址:http://www.cnblogs.com/candy99/p/6103034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!