码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4240 Route Redundancy 最大流

时间:2015-03-10 00:04:03      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

题目链接: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 point B is the ratio of the maximum number of cars that can get from point A to point B in an hour using all routes simultaneously,to the maximum number of cars thar can get from point A to point B in an hour using one route.The minimum redundancy ratio is the number of capacity of the single route with the laegest capacity.
题目描述:The redundancy ratio定义为:(A到B所有路径的流的和)/(A到B一条路径的流的值),求最小的The redundancy ratio(即A到B一条路径上的权值最大)。
算法分析:最大流的求解过程中更新每一条增广路的流的大小即可。
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<cmath>
  6 #include<algorithm>
  7 #include<vector>
  8 #include<queue>
  9 #define inf 0x7fffffff
 10 using namespace std;
 11 const int maxn=1000+10;
 12 const int M = 9999999;
 13 
 14 int n,m,A,B;
 15 int from,to;
 16 int d[maxn];
 17 int one;
 18 struct node
 19 {
 20     int v,flow;
 21     int next;
 22 }edge[M];
 23 int head[maxn],edgenum;
 24 
 25 void add(int u,int v,int flow)
 26 {
 27     edge[edgenum].v=v ;edge[edgenum].flow=flow;
 28     edge[edgenum].next=head[u];
 29     head[u]=edgenum++;
 30 
 31     edge[edgenum].v=u ;edge[edgenum].flow=0;
 32     edge[edgenum].next=head[v];
 33     head[v]=edgenum++;
 34 }
 35 
 36 int bfs()
 37 {
 38     memset(d,0,sizeof(d));
 39     d[from]=1;
 40     queue<int> Q;
 41     Q.push(from);
 42     while (!Q.empty())
 43     {
 44         int u=Q.front() ;Q.pop() ;
 45         for (int i=head[u] ;i!=-1 ;i=edge[i].next)
 46         {
 47             int v=edge[i].v;
 48             if (!d[v] && edge[i].flow>0)
 49             {
 50                 d[v]=d[u]+1;
 51                 Q.push(v);
 52                 if (v==to) return 1;
 53             }
 54         }
 55     }
 56     return 0;
 57 }
 58 
 59 int dfs(int u,int flow)
 60 {
 61     if (u==to || flow==0)
 62     {
 63         if (u==to) one=max(one,flow);
 64         return flow;
 65     }
 66     int cap=flow;
 67     for (int i=head[u] ;i!=-1 ;i=edge[i].next)
 68     {
 69         int v=edge[i].v;
 70         if (d[v]==d[u]+1 && edge[i].flow>0)
 71         {
 72             int x=dfs(v,min(cap,edge[i].flow));
 73             edge[i].flow -= x;
 74             edge[i^1].flow += x;
 75             cap -= x;
 76             if (cap==0) return flow;
 77         }
 78     }
 79     return flow-cap;
 80 }
 81 
 82 double dinic()
 83 {
 84     double sum=0;
 85     one=0;
 86     while (bfs()) sum += (double)dfs(from,inf);
 87     cout<<sum<<" "<<one<<endl;
 88     return sum/(double)one;
 89 }
 90 
 91 int main()
 92 {
 93     int t,D;
 94     scanf("%d",&t);
 95     while (t--)
 96     {
 97         scanf("%d%d%d%d%d",&D,&n,&m,&A,&B);
 98         memset(head,-1,sizeof(head));
 99         edgenum=0;
100         from=A;
101         to=B;
102         int a,b,c;
103         for (int i=0 ;i<m ;i++)
104         {
105             scanf("%d%d%d",&a,&b,&c);
106             add(a,b,c);
107         }
108         double ans=dinic();
109         printf("%d %.3lf\n",D,ans);
110     }
111     return 0;
112 }

 

hdu 4240 Route Redundancy 最大流

标签:

原文地址:http://www.cnblogs.com/huangxf/p/4324867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!