标签:
每日一水~~~
1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #define oo 0x3f3f3f3f 5 #define N 55010 6 using namespace std; 7 8 struct Edge { 9 int u, v, f; 10 Edge( int u, int v, int f ):u(u),v(v),f(f){} 11 }; 12 13 int n, m, src, dst; 14 vector<Edge> edge; 15 vector<int> g[N]; 16 int dep[N], cur[N], qu[N], bg, ed, idc; 17 int sump; 18 19 void init() { 20 for( int i=1; i<=n+m+2; i++ ) 21 g[i].clear(); 22 edge.clear(); 23 sump = 0; 24 idc = 0; 25 } 26 void adde( int u, int v, int f ) { 27 g[u].push_back( edge.size() ); 28 edge.push_back( Edge(u,v,f) ); 29 g[v].push_back( edge.size() ); 30 edge.push_back( Edge(v,u,0) ); 31 } 32 bool bfs() { 33 memset( dep, 0, sizeof(dep) ); 34 qu[bg=ed=1] = src; 35 dep[src] = 1; 36 while( bg<=ed ) { 37 int u=qu[bg++]; 38 for( int t=0; t<g[u].size(); t++ ) { 39 Edge &e = edge[g[u][t]]; 40 if( e.f && !dep[e.v] ) { 41 dep[e.v] = dep[e.u] + 1; 42 qu[++ed] = e.v; 43 } 44 } 45 } 46 return dep[dst]; 47 } 48 int dfs( int u, int a ) { 49 if( u==dst || a==0 ) return a; 50 int remain=a, past=0, na; 51 for( int &t=cur[u]; t<g[u].size(); t++ ) { 52 Edge &e = edge[g[u][t]]; 53 Edge &ve = edge[g[u][t]^1]; 54 if( e.f && dep[e.v]==dep[e.u]+1 && (na=dfs(e.v,min(remain,e.f))) ) { 55 remain -= na; 56 past += na; 57 e.f -= na; 58 ve.f += na; 59 if( !remain ) break; 60 } 61 } 62 return past; 63 } 64 int maxflow() { 65 int flow = 0; 66 while( bfs() ) { 67 memset( cur, 0, sizeof(cur) ); 68 flow += dfs(src,oo); 69 } 70 return flow; 71 } 72 int main() { 73 while( scanf( "%d%d", &n, &m )==2 ) { 74 init(); 75 src = n+m+1; 76 dst = src+1; 77 for( int i=1,c; i<=n; i++ ) { 78 scanf( "%d", &c ); 79 ++idc; 80 adde( idc, dst, c ); 81 } 82 for( int i=1,u,v,p; i<=m; i++ ) { 83 scanf( "%d%d%d", &u, &v, &p ); 84 ++idc; 85 sump += p; 86 adde( src, idc, p ); 87 adde( idc, u, oo ); 88 adde( idc, v, oo ); 89 } 90 printf( "%d\n", sump-maxflow() ); 91 } 92 }
标签:
原文地址:http://www.cnblogs.com/idy002/p/4531109.html