Barricade
Time limit:3000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)Problem DescriptionThe Empire is under attack again. The general of Empire was planning to defend his castle. The land can be seen asNTowns andMRoads, and each road have the same length and connects and the towns. The town numbered1Is where General's castle is located, and the town numberedNIs where the enemies is staying. The general supposes that the enemies would choose a shortest path. He knows his army was not a ready-to-fight and he needs more time. Consequently he decides to put some barricades in some roads to slow down his enemies. Now, he asks your to find a-to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the I-th Road requires wi units of wood. Because of lacking resources, you need to use as less wood as possible. Inputthe first line of input contains an integerT, thenTTest cases follow.
For all test case, in the first line there is integersn(n≤) andm(m≤10000) .
TheI-the Line of the nextMLines describes theI-th Edge with three integersu,v andWwhere0≤w≤ Denoting an edge between u and v of Barricade cost w. Outputfor Each test cases, the output of the minimum wood cost. Sample Input14 2 4 1 3 4 Sample Output4Analysis: The shortest cut maximum flow can be obtained;Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#defineRep (I,m,n) for (i=m;i<=n;i++)#defineRSP (It,s) for (Set<int>::iterator It=s.begin (); It!=s.end (); it++)#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineVI vector<int>#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#definell Long Long#definePi ACOs (-1.0)#definePII pair<int,int>#defineLson L, Mid, rt<<1#defineRson mid+1, R, rt<<1|1Const intmaxn=1e3+Ten;using namespacestd;ll gcd (ll p,ll q) {returnq==0? P:GCD (q,p%q);} ll Qpow (ll p,ll q) {ll F=1; while(q) {if(q&1) f=f*p;p=p*p;q>>=1;}returnF;}intN,m,k,t,h[maxn],tot,vis[maxn],s,cur[maxn],f[maxn],d[maxn],g[maxn];vi EDGE[MAXN];structnode{intx, Y, Z Node () {} node (int_x,int_y,int_z): X (_x), Y (_y), Z (_z) {}}op[10010];structnode{intTo,nxt,cap,flow;} e[10010<<1];voidAddintXintYintz) {e[tot].to=y; E[TOT].NXT=H[x]; E[tot].cap=Z; E[tot].flow=0; H[X]=tot++; E[tot].to=x; E[TOT].NXT=H[y]; E[tot].flow=0; H[y]=tot++;}BOOLBFs () {memset (Vis,0,sizeofvis); Queue<int>p; P.push (s); Vis[s]=1; while(!P.empty ()) { intx=P.front ();p. Pop (); for(inti=h[x];i!=-1; i=e[i].nxt) { intto=e[i].to,cap=e[i].cap,flow=E[i].flow; if(!vis[to]&&cap>flow) {Vis[to]=vis[x]+1; P.push (to); } } } returnvis[t];}voidPR_BFS (ints) { inti; memset (D,inf,sizeofd); memset (Vis,0,sizeofvis); Queue<int>p;p.push (s); vis[s]=1;d [s]=0; while(!P.empty ()) { intQ=p.front ();p. Pop (); vis[q]=0; for(intX:edge[q]) { if(d[x]>d[q]+1) {D[x]=d[q]+1; if(!vis[x]) P.push (x), vis[x]=1; } } } if(s==n) Rep (i,1, N) f[i]=D[i]; ElseRep (I,1, N) g[i]=D[i]; return;}intDfsintXinta) { if(x==t| | a==0)returnA; intans=0, J; for(int&i=cur[x];i!=-1; i=e[i].nxt) { intto=e[i].to,cap=e[i].cap,flow=E[i].flow; if(vis[to]==vis[x]+1&& (J=dfs (To,min (a,cap-flow)) >0) {E[i].flow+=J; E[i^1].flow-=J; Ans+=J; A-=J; if(a==0) Break; } } returnans;}intMax_flow (intSintt) { intflow=0, I; while(BFS ()) {memcpy (cur,h,sizeofcur); Flow+=DFS (S,inf); } returnflow;}intMain () {inti,j,test; scanf ("%d",&test); while(test--) {tot=0; memset (H,-1,sizeofh); scanf ("%d%d",&n,&m); Rep (I,1, N) edge[i].clear (); Rep (I,0, M-1) { intA,b,c; scanf ("%d%d%d",&a,&b,&c); Op[i]=Node (A,B,C); EDGE[A].PB (b), EDGE[B].PB (a); } PR_BFS (n); PR_BFS (1); Rep (I,0, M-1) { intA=op[i].x,b=op[i].y,c=op[i].z; if(f[a]+g[b]+1==f[1]) Add (A,B,C); if(f[b]+g[a]+1==f[1]) Add (B,A,C); } s=n,t=1; printf ("%d\n", Max_flow (s,t)); } //System ("Pause"); return 0;}
2016 Shenyang online race barricade