您的位置:首页 > 其它

hdu 4240 Route Redundancy 最大流

2015-03-09 23:33 295 查看
题目链接: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一条路径上的权值最大)。
算法分析:最大流的求解过程中更新每一条增广路的流的大小即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=1000+10;
const int M = 9999999;

int n,m,A,B;
int from,to;
int d[maxn];
int one;
struct node
{
int v,flow;
int next;
}edge[M];
int head[maxn],edgenum;

void add(int u,int v,int flow)
{
edge[edgenum].v=v ;edge[edgenum].flow=flow;
edge[edgenum].next=head[u];
head[u]=edgenum++;

edge[edgenum].v=u ;edge[edgenum].flow=0;
edge[edgenum].next=head[v];
head[v]=edgenum++;
}

int bfs()
{
memset(d,0,sizeof(d));
d[from]=1;
queue<int> Q;
Q.push(from);
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int i=head[u] ;i!=-1 ;i=edge[i].next)
{
int v=edge[i].v;
if (!d[v] && edge[i].flow>0)
{
d[v]=d[u]+1;
Q.push(v);
if (v==to) return 1;
}
}
}
return 0;
}

int dfs(int u,int flow)
{
if (u==to || flow==0)
{
if (u==to) one=max(one,flow);
return flow;
}
int cap=flow;
for (int i=head[u] ;i!=-1 ;i=edge[i].next)
{
int v=edge[i].v;
if (d[v]==d[u]+1 && edge[i].flow>0)
{
int x=dfs(v,min(cap,edge[i].flow));
edge[i].flow -= x;
edge[i^1].flow += x;
cap -= x;
if (cap==0) return flow;
}
}
return flow-cap;
}

double dinic()
{
double sum=0;
one=0;
while (bfs()) sum += (double)dfs(from,inf);
cout<<sum<<" "<<one<<endl;
return sum/(double)one;
}

int main()
{
int t,D;
scanf("%d",&t);
while (t--)
{
scanf("%d%d%d%d%d",&D,&n,&m,&A,&B);
memset(head,-1,sizeof(head));
edgenum=0;
from=A;
to=B;
int a,b,c;
for (int i=0 ;i<m ;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
double ans=dinic();
printf("%d %.3lf\n",D,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: