题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3549
题意:求1到n的最大流
题解:模版题,直接上Claris的ISAP,效率是一般dfs的十倍,ORZ Claris!!!
1 #include<cstdio> 2 #include<algorithm> 3 #define F(i,a,b) for(int i=a;i<=b;i++) 4 using namespace std; 5 const int N=20,inf=~0U>>2,M=2010; 6 struct edge{int t,f;edge*nxt,*pair;}*g[N],*d[N],pool[M],*cur=pool; 7 struct ISAP{ 8 int n,m,i,S,T,h[N],gap[N],maxflow; 9 void init(int ss,int tt){for(S=ss,T=tt,cur=pool,i=1;i<=T;i++)g[i]=d[i]=NULL,h[i]=gap[i]=0;} 10 void add(int s,int t,int f){ 11 edge*p=cur++;p->t=t,p->f=f,p->nxt=g[s],g[s]=p; 12 p=cur++,p->t=s,p->f=0,p->nxt=g[t],g[t]=p; 13 g[s]->pair=g[t],g[t]->pair=g[s]; 14 } 15 int sap(int v,int flow){ 16 if(v==T)return flow; 17 int rec=0; 18 for(edge*p=d[v];p;p=p->nxt)if(h[v]==h[p->t]+1&&p->f){ 19 int ret=sap(p->t,min(flow-rec,p->f)); 20 p->f-=ret;p->pair->f+=ret;d[v]=p; 21 if((rec+=ret)==flow)return flow; 22 } 23 if(!(--gap[h[v]]))h[S]=T; 24 gap[++h[v]]++;d[v]=g[v]; 25 return rec; 26 } 27 int get_ans(){ 28 for(gap[maxflow=0]=T,i=1;i<=T;i++)d[i]=g[i]; 29 while(h[S]<T)maxflow+=sap(S,inf); 30 return maxflow; 31 } 32 }G; 33 34 int main(){ 35 int t,n,m,x,y,c,ic=1; 36 scanf("%d",&t); 37 while(t--){ 38 scanf("%d%d",&n,&m);//点,边 39 G.init(1,n); 40 F(i,1,m)scanf("%d%d%d",&x,&y,&c),G.add(x,y,c); 41 printf("Case %d: %d\n",ic++,G.get_ans()); 42 } 43 }