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 }

时间: 2024-07-30 00:23:35

hdu 4240 最大流量路径的相关文章

HDU 4240 Route Redundancy 一条流最大的路径

题目来源:HDU 4240 Route Redundancy 题意:求最大流与一条流最大的路径的比值 前者最大流求出 后者是每一条路的最小值再取大 思路:我用的是dinic 可以在DFS的时候在传递一个参数 表示当前增广路可以通过最大的流量 然后当x==t 到达汇点时 在取他们的最大值 #include <cstdio> #include <queue> #include <vector> #include <cstring> #include <al

HDU 4240

http://acm.hdu.edu.cn/showproblem.php?pid=4240 题意:求最大流和流量最大的一条路径的流量的比值 题解:流量最大的路径的流量在dinic的dfs每次搜到终点的时候更新最大值 #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std

HDU 4160 最小路径覆盖

Dolls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1040    Accepted Submission(s): 496 Problem Description Do you remember the box of Matryoshka dolls last week? Adam just got another box of

HDU 1350 最小路径覆盖

Taxi Cab Scheme Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 707    Accepted Submission(s): 336 Problem Description Running a taxi station is not all that simple. Apart from the obvious dem

hdu 4240(最大流+最大流量的路)

Route Redundancy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 625    Accepted Submission(s): 367 Problem Description A city is made up exclusively of one-way steets.each street in the city ha

hdu 4240 Route Redundancy 最大流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4240 A city is made up exclusively of one-way steets.each street in the city has a capacity,which is the minimum of the capcities of the streets along that route. The redundancy ratio from point A to poi

HDU 4240 Route Redundancy

Route Redundancy Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 424064-bit integer IO format: %I64d      Java class name: Main A city is made up exclusively of one-way steets.each street in the city has a ca

hdu 2819 记录路径的二分匹配

题目大意就是给出一个矩阵,每个格子里面要么是0, 要么是1:是否能够经过交换(交换行或者列)使得主对角线上都是1. 其实就行和列的匹配,左边是行,右边是列,然后如果行列交点是1,那么就可以匹配,看是否为完美匹配,然后输出怎么交换的.开始很蒙的,后来仔细去 想,可以这样理解,想要对角线上都是1,那么我们就可以锁定行,来选择列和它匹配,将选择的列移动到和该行形成对角线上是1的位置,比如和第一行匹配的 列,就要移动要第一列,第二行的,就到第二列.其实就是对第i行,找一个第i个数是1的列和它匹配,然后看

hdu 1026(优先队列+路径输出)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17171    Accepted Submission(s): 5495Special Judge Problem Description The Princess has been abducted by the BEelzebub