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
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

题意:求解 最大流 / 图里面最大流量的那一条路 是多少??

http://www.cnblogs.com/yezekun/p/3925768.html正确题解提供者。

题解:此题正确解法不是在dfs时找增广路时更新,那样的话会出两个问题.所以需要先要预处理出最大流量的那条路.然后再求最大流。这样才是正确的。

#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
const int N = 1005;
const int INF = 999999999;
struct Edge{
    int v,w,next;
}edge[N*N];
int head[N];
int level[N];
int tot,max_increase;
void init()
{
    memset(head,-1,sizeof(head));
    tot=0;
}
void addEdge(int u,int v,int w,int &k)
{
    edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
    edge[k].v = u,edge[k].w=0,edge[k].next=head[v],head[v]=k++;
}
int BFS(int src,int des)
{
    queue<int>q;
    memset(level,0,sizeof(level));
    level[src]=1;
    q.push(src);
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        if(u==des) return 1;
        for(int k = head[u]; k!=-1; k=edge[k].next)
        {
            int v = edge[k].v;
            int w = edge[k].w;
            if(level[v]==0&&w!=0)
            {
                level[v]=level[u]+1;
                q.push(v);
            }
        }
    }
    return -1;
}
int dfs(int u,int des,int increaseRoad){
    if(u==des||increaseRoad==0) {
        return increaseRoad;
    }
    int ret=0;
    for(int k=head[u];k!=-1;k=edge[k].next){
        int v = edge[k].v,w=edge[k].w;
        if(level[v]==level[u]+1&&w!=0){
            int MIN = min(increaseRoad-ret,w);
            w = dfs(v,des,MIN);
            if(w > 0)
            {
                edge[k].w -=w;
                edge[k^1].w+=w;
                ret+=w;
                if(ret==increaseRoad){
                    return ret;
                }
            }
            else level[v] = -1;
            if(increaseRoad==0) break;
        }
    }
    if(ret==0) level[u]=-1;
    return ret;
}
int Dinic(int src,int des)
{
    int ans = 0;
    while(BFS(src,des)!=-1) ans+=dfs(src,des,INF);
    return ans;
}
int d,n,m,src,des;
bool vis[N];
void dfs1(int u,int ans){
    if(u==des){
        max_increase = max(max_increase,ans);
        return ;
    }
    for(int k=head[u];k!=-1;k=edge[k].next){
        int v = edge[k].v,w = edge[k].w;
        if(!vis[v]){
            vis[v] = true;
            dfs1(v,min(w,ans)); ///最大流量由最小容量边决定
            vis[v] = false;
        }
    }
}
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--){
        init();
        max_increase = -1;
        scanf("%d%d%d%d%d",&d,&n,&m,&src,&des);
        for(int i=1;i<=m;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addEdge(u,v,w,tot);
        }
        memset(vis,false,sizeof(vis));
        dfs1(src,99999999); ///deal
        int max_flow = Dinic(src,des);
        printf("%d %.3lf\n",d,max_flow*1.0/max_increase);
    }
    return 0;
}
时间: 2024-08-17 19:07:46

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 2686 费用流 / 双线程DP

题意:给一个方阵,求从左上角出到右下角(并返回到起点),经过每个点一次不重复,求最大获益(走到某处获得改点数值),下来时每次只能向右或向下,反之向上或向左. 俩种解法: 1  费用流法:思路转化:从左上角流出2的流量,(表示走俩条路),归于右下角,可以走就有边(右和下),权为负的费用,源点连起点,汇点连终点,流量为2. 除源汇外所有点一分为2,Y向X对应点有流量1的边,之前边为X到Y的(原图),这样处理解决每个点只最多走一次(除了源汇外)(X部只出,Y部要出必先回到X对应点).跑最小费用最大流即

hdu 4888 最大流给出行列和求矩阵

第一步,考虑如何求是否有解.使用网络流求解,每一行和每一列分别对应一个点,加上源点和汇点一共有N+M+2个点.有三类边: 1. 源点 -> 每一行对应的点,流量限制为该行的和 2. 每一行对应的点 -> 每一列对应的点,流量限制为 K 3. 每一列对应的点 -> 汇点,流量限制为该列的和 对上图做最大流,若源点出发的边和到达汇点的边全都满流,则有解,否则无解.若要求构造方案,则 (i,j) 对应的整数就是行 i–> 列 j 的流量. 第二步,考虑解是否唯一.显然,解唯一的充分必要条

hdu 4975最大流与4888类似但是有很吊的优化最大流

//来自潘神的优化 #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define inf 0x3fffffff #define N 1100 struct node { int u,v,w,next; }bian[N*N*4]; int head[N],yong,dis[N],work[N]; void init(){ yong=0; memset(head,-1,si

Leapin&#39; Lizards (hdu 2732 最大流)

Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1433    Accepted Submission(s): 586 Problem Description Your platoon of wandering lizards has entered a strange room in the labyr

网络最大流增广路模板(EK &amp; Dinic)

EK算法: int fir[maxn]; int u[maxm],v[maxm],cap[maxm],flow[maxm],nex[maxm]; int e_max; int p[maxn],q[maxn],d[maxn]; void add_edge(int _u,int _v,int _w) { int e; e=e_max++; u[e]=_u;v[e]=_v;cap[e]=_w; nex[e]=fir[u[e]];fir[u[e]]=e; e=e_max++; u[e]=_v;v[e]=

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 最大流量路径

题意弄了半天: 给出一个有向图,带边权,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