POJ 1273 最大流模板题

来源:互联网 发布:美化你的声音 知乎 编辑:程序博客网 时间:2024/05/22 09:48

看了三天的网络流,总算是切掉一道题了... 

还是要安静下来耐心的,自信的切题啊~~嘿嘿~

#include<iostream>#include<cstdio>#include<string>using namespace std;struct edge{int f,c;}g[222][222];struct node{int l,p,a;}list[222];int N,M,ans,s,t;void init(){  int u,v,c;  memset( g,0,sizeof(g) );  for( int i=0;i<N;i++ )  {  scanf( "%d%d%d",&u,&v,&c );  g[u][v].c+=c;   } s=1;t=M;ans=0;}int find(){ int i=1; while( i<=M&&(list[i].p!=0||list[i].l==0) )i++; if( i>M ) return 0; else return i;}bool ford(){  memset( list,0,sizeof(list) );  list[s].l=s;  list[s].a=9999999;  while( list[t].l==0 )  {  int i=find();  if( i==0 ) return true;  for( int j=1;j<=M;j++ )  {  if( list[j].l==0 && (g[i][j].c||g[j][i].c) ){   if( g[i][j].c-g[i][j].f>0 ){   list[j].l=i;       list[j].a=min( list[i].a,g[i][j].c-g[i][j].f );    }    if( g[j][i].f>0 ){   list[j].l=-i;       list[j].a=min( list[i].a,g[j][i].f );      }    }   }   list[i].p=1; } ans+=list[t].a; return false;}void change(){  int j,m,a=list[t].a;  m=t;  while( m!=s )  { j=m;m=abs(list[m].l);  if( list[m].l>0 ) g[m][j].f+=a;  if( list[m].l<0 ) g[j][m].f-=a; }}void work(){  while(1)  {bool p=ford(); if( p ) break; else change(); } printf( "%d\n",ans );}int main(){ while( scanf("%d%d",&N,&M)!=EOF ) {    init();    work();  } return 0;}


原创粉丝点击