hdu 4240 最大流量路径
题意弄了半天:
给出一个有向图,带边权,src,dst. 求出src到dst的最大流,再求出从src到dst流量最大的路径的流量,求它们的比值。
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <vector> 5 #define oo 0x3f3f3f3f 6 #define maxn 1010 7 using namespace std; 8 9 struct Edge { 10 int u, v, f, cap; 11 Edge( int u, int v, int f, int cap ):u(u),v(v),f(f),cap(cap){} 12 }; 13 struct Dinic { 14 int n, src, dst; 15 vector<Edge> edge; 16 vector<int> g[maxn]; 17 int dep[maxn], cur[maxn], sig; 18 19 void init( int n, int src, int dst ) { 20 this->n = n; 21 this->src = src; 22 this->dst = dst; 23 for( int u=1; u<=n; u++ ) 24 g[u].clear(); 25 edge.clear(); 26 } 27 void add_edge( int u, int v, int f ) { 28 g[u].push_back( edge.size() ); 29 edge.push_back( Edge(u,v,f,f) ); 30 g[v].push_back( edge.size() ); 31 edge.push_back( Edge(v,u,0,f) ); 32 } 33 bool bfs() { 34 queue<int> qu; 35 memset( dep, 0, sizeof(dep) ); 36 qu.push( src ); 37 dep[src] = 1; 38 while( !qu.empty() ) { 39 int u=qu.front(); 40 qu.pop(); 41 for( int t=0; t<g[u].size(); t++ ) { 42 Edge &e = edge[g[u][t]]; 43 if( e.f && !dep[e.v] ) { 44 dep[e.v] = dep[e.u]+1; 45 qu.push( e.v ); 46 } 47 } 48 } 49 return dep[dst]; 50 } 51 int dfs( int u, int a, int minc ) { 52 if( u==dst || a==0 ) { 53 sig = max( minc, sig ); 54 return a; 55 } 56 int remain=a, past=0, na; 57 for( int &t=cur[u]; t<g[u].size(); t++ ) { 58 Edge &e = edge[g[u][t]]; 59 Edge &ve = edge[g[u][t]^1]; 60 if( dep[e.v]==dep[e.u]+1 && (na=dfs(e.v,min(remain,e.f),min(minc,e.cap))) ) { 61 remain -= na; 62 past += na; 63 e.f -= na; 64 ve.f += na; 65 if( remain==0 ) break; 66 } 67 } 68 return past; 69 } 70 void maxflow( int &tot, int &sig ) { 71 tot = sig = 0; 72 this->sig = 0; 73 while( bfs() ) { 74 memset( cur, 0, sizeof(cur) ); 75 tot += dfs(src,oo,oo); 76 } 77 sig = this->sig; 78 } 79 }; 80 81 int n, m; 82 Dinic D; 83 int main() { 84 int T; 85 scanf( "%d", &T ); 86 while( T-- ) { 87 int cas, src, dst; 88 scanf( "%d%d%d%d%d", &cas, &n, &m, &src, &dst ); 89 src++, dst++; 90 D.init( n, src, dst ); 91 for( int i=1,u,v,w; i<=m; i++ ) { 92 scanf( "%d%d%d", &u, &v, &w ); 93 u++, v++; 94 D.add_edge( u, v, w ); 95 } 96 int tot, sig; 97 D.maxflow( tot, sig ); 98 printf( "%d %.3lf\n", cas, double(tot)/double(sig) ); 99 } 100 }