hdu 4240 Route Redundancy(求起点到终点的路径中流量最大的路径的流量)

探讨了如何通过最大流算法及DFS遍历解决城市交通网络中起点到终点的最大通行能力与路径冗余比的问题,并提供了详细的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Route Redundancy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 631    Accepted Submission(s): 368


Problem Description
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.
 

Input
The first line of input contains asingle integer P,(1<=P<=1000),which is the number of data sets that follow.Each data set consists of several lines and represents a directed graph with positive integer weights.

The first line of each data set contains five apace separatde integers.The first integer,D is the data set number. The second integer,N(2<=N<=1000),is the number of nodes inthe graph. The thied integer,E,(E>=1),is the number of edges in the graph. The fourth integer,A,(0<=A<N),is the index of point A.The fifth integer,B,(o<=B<N,A!=B),is the index of point B.

The remaining E lines desceibe each edge. Each line contains three space separated in tegers.The First integer,U(0<=U<N),is the index of node U. The second integer,V(0<=v<N,V!=U),is the node V.The third integer,W (1<=W<=1000),is th capacity (weight) of path from U to V.
 

Output
For each data set there is one line of output.It contains the date set number(N) follow by a single space, followed by a floating-point value which is the minimum redundancy ratio to 3 digits after the decimal point.
 

Sample Input
  
  
1 1 7 11 0 6 0 1 3 0 3 3 1 2 4 2 0 3 2 3 1 2 4 2 3 4 2 3 5 6 4 1 1 4 6 1 5 6 9
 

Sample Output
  
  
1 1.667
 

Source
题意:给t,n,m,a,b,t是样例累加器,n代表有0~n-1的点,m是边(单向),a是起点b是终点,求最大流/起点到终点的路径中流量最大的路径的流量

思路:最大流直接求,可是起点到终点的路径中流量最大的路径的流量如何求呢? 这里参考了这位大神的博客。

点击打开链接

用dfs进行一次所有起点到终点的路径的遍历,找出其中最小的容量是所有路径中最大的那一个容量即可

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 2050
#define INF 99999999
struct Edge
{
    int v,next,cap;
} edge[N*N];
int cnt,head[N],d[N],vis[N];
int num;
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
}
void addedge(int u,int v,int cap)
{
    edge[cnt].v=v,edge[cnt].cap=cap;
    edge[cnt].next=head[u],head[u]=cnt++;
    edge[cnt].v=u,edge[cnt].cap=0;
    edge[cnt].next=head[v],head[v]=cnt++;
}
int bfs(int s,int t)
{
    memset(d,-1,sizeof(d));
    d[s]=0;
    queue<int>q;
    q.push(s);
    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,cap=edge[i].cap;
            if(d[v]==-1&&cap>0)
            {
                d[v]=d[u]+1;
                q.push(v);
            }
        }
    }
    return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
    if(s==t||f==0) return f;
    int flow=0;
    for(int i=head[s]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v,cap=edge[i].cap;
        if(d[v]==d[s]+1&&cap>0)
        {
            int x=min(f-flow,cap);
            x=dfs(v,t,x);
            flow+=x;
            edge[i].cap-=x;
            edge[i^1].cap+=x;
        }
    }
    if(!flow) d[s]=-2;
    return flow;
}
int Dinic(int s,int t)
{
    int flow=0,f;
    while(bfs(s,t))
    {
        while(f=dfs(s,t,INF))
                flow+=f;
    }
    return flow;
}
void get_dfs(int u,int f,int t)
{
    if(f<num) return;
    if(u==t)
    {
        num=max(num,f);
        return;
    }
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!vis[v])
        {
            vis[v]=1;
            get_dfs(v,min(f,edge[i].cap),t);
            vis[v]=0;
        }
    }
}
int main()
{
    int T,t,n,m,a,b;
    int u,v,w;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d %d %d %d %d",&t,&n,&m,&a,&b);
        while(m--)
        {
            scanf("%d %d %d",&u,&v,&w);
            addedge(u,v,w);
        }
        num=0;
        memset(vis,0,sizeof(vis));
        vis[a]=1;
        get_dfs(a,INF,b);
        int ans=Dinic(a,b);
        double aa=ans*1.0/num;
        printf("%d %.3lf\n",t,aa);
    }
    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值