ACM/ICPC Beijing Station D problem
Maximum network flow
And the students discussed for a while, or Xiang578 wit ...
/************************************************author: Zhou Jitao Email: [Email protected]created time:2015/ 11/19 22:32:32file name:acm\acm.cpp*************************************************/#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<time.h>using namespacestd;Const intMAXN = -*2+Ten;Const intINF =0x7FFFFFFF;structedge{int from, to, cap, flow; Edge (intUintVintCintf): from(U), to (v), Cap (c), Flow (f) {}};vector<Edge>Edges;vector<int>G[MAXN];BOOLVIS[MAXN];intD[MAXN];intCUR[MAXN];intN, m, S, T;intS;intu[10000+Ten],v[10000+Ten],cost[10000+Ten];intP[MAXN],D[MAXN];voidinit () { for(inti =0; i < MAXN; i++) g[i].clear (); Edges.clear ();}voidAddedge (int from,intTo,intcap) {Edges.push_back (Edge ( from, to, Cap,0)); Edges.push_back (Edge to, from,0,0)); intW =edges.size (); g[ from].push_back (W-2); G[to].push_back (W-1);}BOOLBFS () {memset (Vis,0,sizeof(VIS)); Queue<int>p; Q.push (s); D[s]=0; Vis[s]=1; while(!Q.empty ()) { intx =Q.front (); Q.pop (); for(inti =0; I<g[x].size (); i++) {Edge e=Edges[g[x][i]]; if(!vis[e.to] && e.cap>e.flow) {vis[e.to]=1; D[e.to]= D[x] +1; Q.push (e.to); } } } returnvis[t];}intDasointXinta) { if(x = = T | | a = =0) returnA; intFlow =0, F; for(int&i = Cur[x]; I<g[x].size (); i++) {Edge e=Edges[g[x][i]]; if(d[x]+1= = d[e.to]&& (F=dfs (E.to,min (a,e.cap-e.flow)) >0) {Edges[g[x][i]].flow+=F; Edges[g[x][i]^1].flow-=F; Flow+=F; A-=F; if(a==0) Break; } } if(!flow) d[x] =-1; returnflow;}intDinic (intSintt) { intFlow =0; while(BFS ()) {memset (cur,0,sizeof(cur)); Flow+=DFS (S, INF); } returnflow;}intMain () {intT; scanf ("%d",&T); while(t--) {init (); scanf ("%d%d%d",&n,&m,&R); for(intI=1; i<=m;i++) scanf ("%d%d%d",&u[i],&v[i],&Cost[i]); for(intI=1; i<=n;i++) scanf ("%d",&P[i]); for(intI=1; i<=n;i++) scanf ("%d",&D[i]); for(intI=1; i<=n;i++) Addedge (i,i+N,d[i]); for(intI=1; i<=n;i++) Addedge (0, I,p[i]); for(intI=1; i<=m;i++) Addedge (u[i]+N,v[i],cost[i]); Addedge (S+n,n+n+1, INF); S=0; t=n+n+1; printf ("%d\n", Dinic (s,t)); } return 0;}
Hihocoder 1252 Kejin Game