POJ 3204 calculates the edge that increases the maximum flow

Source: Internet
Author: User

This question is a bit skillful.

We have obtained the largest stream. How can we know which edge can increase the traffic?


If e (U, V): S-> U path has the remaining capacity, and V-> t path has the remaining capacity.

If an edge is in the preceding situation, expand the edge e to increase the maximum flow.

It should be very understandable. It is easy to identify all vertices in the S set by using the DFS through the Source Vertex.

So how can we get .. V to T?

Similarly, if we use T to DFS, as long as there are points V to T with the remaining capacity, then V is the point of the set T in DFS (V );

From the particularity of the edge, we can find that the reverse edge of the current edge k is k ^ 1, and we can determine the remaining capacity through k ^ 1.

This is marked as 2.

After scanning the last side, the edge connecting the two sets can be resized.

PS: do not include reverse edges ....

#include<iostream>#include<cstdio>#define MN 1111#define MM 22222#define CC(a) memset(a,0,sizeof(a))#define FF(i,m) for( int i=0;i<m;i++ )#define INF 0x0FFFFFFFtemplate<class T>inline void checkmin( T &a,T b ){ if( a>b||a==-1 ) a=b; }using namespace std;struct edge{   int u,pos,c,f,next;}E[MM];int N,M,s,t,alloc;int head[MN],gap[MN],dis[MN],cur[MN],pre[MN];void addEdge( int u,int v,int c,int cc=0 ){  E[alloc].u=u;  E[alloc].pos=v;  E[alloc].c=c;  E[alloc].next=head[u];  head[u]=alloc++;  E[alloc].u=v;  E[alloc].pos=u;  E[alloc].c=cc;  E[alloc].next=head[v];  head[v]=alloc++;}void setG(){  CC(E);alloc=0;  s=0;t=N-1;  memset( head,-1,sizeof(head) ); int u,v,c; while( M-- ){    scanf("%d%d%d",&u,&v,&c);    addEdge( u,v,c );   }}int sap(){ CC(dis),CC(gap); FF(i,t+1) cur[i]=head[i]; int u=pre[s]=s,maxflow=0,aug=-1; gap[0]=t+1; while( dis[s]<=t ){loop:    for( int &i=cur[u];i!=-1;i=E[i].next )    {   int v=E[i].pos;   if( E[i].c-E[i].f && dis[u]==dis[v]+1 )   { pre[v]=u; checkmin(aug,E[i].c-E[i].f); u=v; if( v==t ) { maxflow+=aug; for( u=pre[u];v!=s;v=u,u=pre[u] )  E[cur[u]].f+=aug,E[cur[u]^1].f-=aug; aug=-1;  }  goto loop;}   }   int mind=t;   for( int i=head[u];i!=-1;i=E[i].next )   {   int v=E[i].pos;   if( E[i].c-E[i].f && mind>dis[v] )   cur[u]=i,mind=dis[v];      }      if( --gap[dis[u]]==0 )break;      gap[dis[u]=mind+1]++;      u=pre[u];  }  return maxflow;}int flag[MN];void dfss( int cur ){  flag[cur]=1;  for( int v=head[cur];v!=-1;v=E[v].next )  if( !flag[E[v].pos]&&(E[v].c-E[v].f) )   dfss(E[v].pos);}void dfst( int cur ){  flag[cur]=2;  for( int v=head[cur];v!=-1;v=E[v].next )  if( !flag[E[v].pos]&&(E[v^1].c-E[v^1].f) )   dfst(E[v].pos);}int main(){ while( scanf("%d%d",&N,&M)!=EOF ) {    setG();    int ans=0;    int maxflow=sap();    //printf( "%d\n",maxflow );    CC(flag);    dfss(s);dfst(t);    for( int i=0;i<alloc;i+=2 )    if( flag[E[i].u]==1&&flag[E[i].pos]==2&&E[i].c==E[i].f )       ans++;   printf( "%d\n",ans );  } return 0;}/*4 60 1 40 2 21 2 21 3 12 3 72 0 1ans:3*/

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.