Dinic to solve the problem of network flow template. Queue with STL on Tle ...
1 /*3061*/2#include <iostream>3#include <string>4#include <map>5#include <queue>6#include <Set>7#include <stack>8#include <vector>9#include <deque>Ten#include <algorithm> One#include <cstdio> A#include <cmath> -#include <ctime> -#include <cstring> the#include <climits> -#include <cctype> -#include <cassert> -#include <functional> +#include <iterator> -#include <iomanip> + using namespacestd; A //#pragma COMMENT (linker, "/stack:102400000,1024000") at - #defineSTI set<int> - #defineStpii Set<pair<int, int> > - #defineMpii map<int,int> - #defineVI vector<int> - #definePII pair<int,int> in #defineVpii vector<pair<int,int> > - #defineRep (I, A, n) for (int i=a;i<n;++i) to #definePer (i, a, n) for (int i=n-1;i>=a;--i) + #defineCLR Clear - #definePB Push_back the #defineMP Make_pair * #defineFIR First $ #defineSEC SecondPanax Notoginseng #defineAll (x) (x). Begin (), (x). End () - #defineSZ (x) ((int) (x). Size ()) the #defineLson L, Mid, rt<<1 + #defineRson mid+1, R, rt<<1|1 A the Const intINF =1e9; + Const intMAXN =505; - Const intMaxe =1111111; $ intV[maxe], F[maxe], nxt[maxe]; $ intHEAD[MAXN], DIS[MAXN]; - intHEAD_[MAXN]; - intQ[MAXN]; the ints, t; - intN, M;Wuyi the voidInitintn_) { -Memset (Head,-1,sizeof(head)); Wus = m =0; -n = n_ +2; Aboutt = n-1; $ } - - voidAddedge (intUintVintc) { -V[M] =v; AF[M] =C; +NXT[M] =Head[u]; theHead[u] = m++; - $V[M] =u; theF[M] =0; theNXT[M] =Head[v]; theHEAD[V] = m++; the } - in BOOLBFs () { the intU, V, K; the intL =0, r =0; About thememset (DIS,-1,sizeof(DIS)); theDis[s] =0; theq[r++] =s; + - while(L <r) { theU = q[l++];Bayi for(K=head[u]; k!=-1; k=Nxt[k]) { thev =V[k]; the if(F[k] && dis[v]<0) { -DIS[V] = Dis[u] +1; - if(v = =t) the return false; theq[r++] =v; the } the } - } the the return true; the }94 the intDfsintUinta) { the if(U = =t) the returnA;98 About intV, TMP; - 101 for(int& K=head_[u]; k!=-1; k=Nxt[k]) {102v =V[k];103 if(F[k] && dis[v]==dis[u]+1&& (Tmp=dfs (V, Min (A, f[k])) >0) {104F[K]-=tmp; thef[k^1] +=tmp;106 returntmp;107 }108 }109 the return 0;111 } the 113 intDinic () { the intRET =0, TMP; the the while(1) {117 if(BFS ())118 Break;119memcpy (Head_, head,sizeof(head)); - while(TMP = DFS (S, INF))! =0)121RET + =tmp;122 }123 124 returnret; the }126 127 intMain () { -Ios::sync_with_stdio (false);129 #ifndef Online_judge theFreopen ("data.in","R", stdin);131Freopen ("Data.out","W", stdout); the #endif133 134 intN_, m_;135 intu, v;136 inttot, ans, x;137 138 while(SCANF ("%d%d", &n_, &m_)! =EOF) {139 init (n_); $tot =0;141Rep (I,1, n_+1) {142scanf"%d", &x);143 if(X >0) {144 Addedge (S, I, x);145Tot + =x;146}Else if(X <0) {147Addedge (i, T,-x);148 }149 } Max while(m_--) {151scanf"%d%d", &u, &v); the Addedge (U, V, INF);153 }154Ans =dinic ();155printf"%d\n", tot-ans);156 }157 158 #ifndef Online_judge159printf"Time =%d.\n", (int) clock ()); the #endif161 162 return 0;163}
"Hdoj" 3061 Battle