再多的言語,措辭都是空洞無力的,唯有行動才能改變現狀,acm本質就是一場競賽,唯有拼搏時才能退役無悔
阿新 • • 發佈:2018-12-16
熟悉dinic的寫法
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; struct Edge { int next,w,to; }e[2005]; int hea[50],deep[50],dl[50]; int t,n,m,cnt; void add(int u,int v,int w) { e[cnt].w=w; e[cnt].to=v; e[cnt].next=hea[u]; hea[u]=cnt++; } bool bfs(int S,int T) { memset(deep,0,sizeof(deep)); deep[S]=1; dl[1]=S; int head=0,tail=1; while(head!=tail) { int v=dl[++head]; for(int i=hea[v];i!=-1;i=e[i].next) { if(e[i].w&&!deep[e[i].to]) { deep[e[i].to]=deep[v]+1; dl[++tail]=e[i].to; } } } return deep[T]; } int aim; int dfs(int now,int fl) { if(now==aim||fl==0) return fl; int f=0; for(int i=hea[now];i!=-1;i=e[i].next) { if(e[i].w&&deep[e[i].to]==deep[now]+1) { int x=dfs(e[i].to,min(fl,e[i].w)); e[i].w-=x; e[i^1].w+=x; fl-=x; f+=x; } } if(!f) deep[now]=-2; return f; } int maxflow(int S,int T) { aim=T; int ans=0; while(bfs(S,T)) { ans+=dfs(S,1<<30); } return ans; } int main() { scanf("%d",&t); int cas=0; while(t--) { cnt=0; memset(hea,-1,sizeof(hea)); scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,0); } printf("Case %d: %d\n",++cas,maxflow(1,n)); } }