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

题目来源:HDU 4240 Route Redundancy

题意:求最大流与一条流最大的路径的比值 前者最大流求出 后者是每一条路的最小值再取大

思路:我用的是dinic 可以在DFS的时候在传递一个参数 表示当前增广路可以通过最大的流量 然后当x==t 到达汇点时 在取他们的最大值

#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 610;
const int INF = 999999999;
int ans;
struct Edge
{
    int from, to, cap, flow;
    Edge(){}
    Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow){}
};

struct Point
{
    double d, x, y, r;
    Point(){}
    Point(double d, double x, double y, double r) : d(d), x(x), y(y), r(r){}
}a[maxn];
int n, m, s, t;
vector <Edge> edges;
vector <int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];void AddEdge(int from, int to, int cap)
{
    edges.push_back(Edge(from, to, cap, 0));
    edges.push_back(Edge(to, from, 0, 0));
    m = edges.size();
    G[from].push_back(m-2);
    G[to].push_back(m-1);
}
bool BFS()
{
    memset(vis, 0, sizeof(vis));
    queue <int> Q;
    Q.push(s);
    d[s] = 0;
    vis[s] = 1;
    while(!Q.empty())
    {
        int x = Q.front(); Q.pop();
        for(int i = 0; i < G[x].size(); i++)
        {
            Edge& e = edges[G[x][i]];
            if(!vis[e.to] && e.cap > e.flow)
            {
                vis[e.to] = 1;
                d[e.to] = d[x] + 1;
                Q.push(e.to);
            }
        }
    }
    return vis[t];
}
int DFS(int x, int a, int v)
{
	if(x == t)
	{
		ans = max(ans, v);
		return a;
	}
    if(a == 0)
    {
		return 0;
    }
    int flow = 0, f;
    for(int& i = cur[x]; i < G[x].size(); i++)
    {
        Edge& e = edges[G[x][i]];
        if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap-e.flow), min(v, e.cap))) > 0)
        {
            e.flow += f;
            edges[G[x][i]^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0)
                break;
        }
    }
    return flow;
}

int Maxflow()
{
    int flow = 0;
    while(BFS())
    {
        memset(cur, 0, sizeof(cur));
        flow += DFS(s, INF, INF);
    }
    return flow;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int cas, k;
        scanf("%d %d %d %d %d", &cas, &n, &k, &s, &t);
        edges.clear();
        ans = 0;
        for(int i = 0; i < n; i++)
            G[i].clear();
        for(int i = 0; i < k; i++)
        {
            int u, v, w;
            scanf("%d %d %d", &u, &v, &w);
            AddEdge(u, v, w);
        }
        int flow = Maxflow();
        printf("%d %.3f\n", cas, (double)flow/ans);
    }
    return 0;
}

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

时间: 2024-10-28 23:36:09

HDU 4240 Route Redundancy 一条流最大的路径的相关文章

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

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 3667 Transportation(网络流之费用流)

题目地址:HDU 3667 这题的建图真是巧妙...为了保证流量正好达到k,需要让每一次增广到的流量都是1,这就需要把每一条边的流量都是1才行.但是每条边的流量并不是1,该怎么办呢.这个时候可以拆边,反正c最多只有5,拆成5条流量为1的边.但是这时候费用怎么办呢,毕竟平方的关系不能简单把每一条边加起来.这时候可以把拆的边的流量设为1,3,5,7,9.如果经过了3个流量,那就肯定会流1,3,5,费用为9,是3的平方,同理,其他的也是如此.然后按照给出的边建图跑一次费用流就可以了. 代码如下: #i

hdu 3549 Flow Problem (网络最大流)

Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 6674    Accepted Submission(s): 3112 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, yo

hdu 4289 Control(网络流 最大流+拆点)(模板)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1545    Accepted Submission(s): 677 Problem Description You, the head of Department o

HDU 2686 Matrix(最大费用最大流+拆点)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 和POJ3422一样 删掉K把汇点与源点的容量改为2(因为有两个方向的选择)即可 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int ma

hdu 4862 Jump 上下界费用流

对于每个点拆点成为两个点a,b,连接a到b的上界为1,下界为1的边,保证用过一次且仅一次. 然后若点u可到达点v,则连接即可.建成了一个上下界网络,将下界拆出去,求最大费用最大流就好. #include <stdio.h> #include <iostream> #include <string.h> using namespace std; const int N=800; const int MAXE=200000; const int inf=1<<3