Poj2455 secret milking machine (Binary online Diagram for maximum Stream)

Source: Internet
Author: User

An undirected graph contains N vertices, m edges, and each edge has a weight value, and each edge can only be used once. It is required to find a path from 1 to n for T, minimize the maximum value of the Weight Value in the edge passing through the T path.

Idea: the binary edge weight is put online, and the maximum stream is obtained through multiple diagrams to determine whether max_flow meets T. For details, see the code:

#include<cstdio>#include<cstring>#include<queue>#define find_min(a,b) a<b?a:b#define find_max(a,b) a>b?a:busing namespace std;const int N = 210;const int MAX = (int)10e7;struct Edge{int s,e,v,next;}edge[2*N*N],mat[N*N];int e_num,head[N],d[N],sp,tp;int n,m;void AddEdge(int a,int b,int c){edge[e_num].s=a; edge[e_num].e=b; edge[e_num].v=c;edge[e_num].next=head[a]; head[a]=e_num++;edge[e_num].s=b; edge[e_num].e=a; edge[e_num].v=c;edge[e_num].next=head[b]; head[b]=e_num++;}int bfs(){queue <int> q;memset(d,-1,sizeof(d));d[sp]=0;q.push(sp);while(!q.empty()){int cur=q.front();q.pop();for(int i=head[cur];i!=-1;i=edge[i].next){int u=edge[i].e;if(d[u]==-1 && edge[i].v>0){d[u]=d[cur]+1;q.push(u);}}}return d[tp] != -1;}int dfs(int a,int b){int r=0;if(a==tp)return b;for(int i=head[a];i!=-1 && r<b;i=edge[i].next){int u=edge[i].e;if(edge[i].v>0 && d[u]==d[a]+1){int x=find_min(edge[i].v,b-r);x=dfs(u,x);r+=x;edge[i].v-=x;edge[i^1].v+=x;}}if(!r)d[a]=-2;return r;}int dinic(int sp,int tp){int total=0,t;while(bfs()){while(t=dfs(sp,MAX))total+=t;}return total;}void init(int index){    int i,j;    e_num=0;    memset(head,-1,sizeof(head));    for(i=1;i<=m;i++){        if(mat[i].v<=index)AddEdge(mat[i].s,mat[i].e,1);    }}int main(){    int t_num,i,j,a,b,c;    while(~scanf("%d%d%d",&n,&m,&t_num)){        sp=1;tp=n;        int high=-1,low=MAX;        for(i=1;i<=m;i++){            scanf("%d%d%d",&a,&b,&c);            high=find_max(high,c);            low=find_min(low,c);            mat[i].s=a; mat[i].e=b; mat[i].v=c;        }        int mid,ans;        while(low<=high){            mid=(low+high)/2;            init(mid);            ans=dinic(sp,tp);            if(ans<t_num)low=mid+1;            else high=mid-1;        }        printf("%d\n",low);    }    return 0;}

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.