zoukankan      html  css  js  c++  java
  • POJ2455Secret Milking Machine[最大流 无向图 二分答案]

    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就不对,每次重新建图就对了,不知道为什么(貌似因为f没有清0)

    无向图的处理,反向边的容量也是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
    ",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){//应该可以这里处理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
    ",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
    ",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
    ",l,r,mid);
            build(mid);
            if(dinic(mid)>=T) ans=min(ans,mid),r=mid-1;
            else l=mid+1;
        }
        printf("%d",ans);
    }
     
     
  • 相关阅读:
    JS 加载html 在IE7 IE8下 可调试
    css 实现三角形 实现过程
    JSONP 理解 和 实例 讲解
    js 闭包 理解
    常见input输入框 点击 发光白色外阴影 focus
    常用垂直居中 随窗口改变一直居中布局 实例
    修改wampserver 默认localhost 和phpmyadmin 打开链接
    validate jquery 注册页面使用实例 详解
    mysql 查询表结构 查询索引
    wampserver 绑定域名 外部可以正常访问
  • 原文地址:https://www.cnblogs.com/candy99/p/6103034.html
Copyright © 2011-2022 走看看