HDU 4240 --Route Redundancy【最大流dinic && DFS】

44 篇文章 0 订阅

Route Redundancy

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


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

意:在城市A->B之间通过所有路径一小时之内能通过最大的车辆(maxflow)/所有边上通过最大车流量(cap)的那条叫做redundancy ratio。最小的redundancy ratio是前者最大的车流量的那一条(cap),问minimum redundancy ratio是多少。(从A到B的最大流 ) / (A到 B 一条路径上可通过的最大容量)。


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define INF 0x3f3f3f3f
#define maxn 2000
#define maxm 4000000
using namespace std;
int N, E, A, B;

int head[maxn], cur[maxn], cnt;
int vis[maxn], dist[maxn];
int mark[maxn];
int maxs;

struct node{
    int u, v, cap, flow, next;
};

node edge[maxm];

void init(){
    cnt = 0;
    memset(head, -1,sizeof(head));
    memset(mark, 0, sizeof(mark));
}

void addedge(int u, int v, int w){
    edge[cnt] = {u, v, w, 0, head[u]};
    head[u] = cnt++;
    edge[cnt] = {v, u, 0, 0, head[v]};
    head[v] = cnt++;
}

void getmap(){
    int a, b, c;
    while(E--){
        scanf("%d%d%d", &a, &b, &c);
        addedge(a, b, c);
    }
    return ;
}

bool BFS(int st, int ed){
    queue<int>q;
    memset(vis, 0, sizeof(vis));
    memset(dist, -1, sizeof(dist));
    vis[st] = 1;
    dist[st] = 0;
    q.push(st);
    while(!q.empty()){
        int u =q.front();
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            node E = edge[i];
            if(!vis[E.v] && E.cap > E.flow){
                vis[E.v] = 1;
                dist[E.v] = dist[u] + 1;
                if(E.v == ed) return true;
                q.push(E.v);
            }
        }
    }
    return false;
}

int DFS(int x, int ed, int a){
    if(x == ed || a == 0)
        return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next){
        node &E = edge[i];
        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
            E.flow += f;
            edge[i ^ 1].flow -= f;
            a -= f;
            flow += f;
            if(a == 0) break;
        }
    }
    return flow;
}

int maxflow(int st, int ed){
    int flowsum = 0;
    while(BFS(st, ed)){
        memcpy(cur, head, sizeof(head));
        flowsum += DFS(st, ed, INF);
    }
    return flowsum;
}

void DFSGET(int u, int mins){//搜索一条路径上可通过的最大容量
    if(mins < maxs) return ;
    if(u == B && mins > maxs){
            maxs = mins;
    }
    for(int i = head[u]; i != -1; i = edge[i].next){
        int v = edge[i].v;
        if(!mark[v]){
            mark[v] = 1;
            //mins = min(mins, edge[i].cap);//这样写就一直错,醉了醉了
            DFSGET(v, mins < edge[i].cap ? mins : edge[i].cap);
            mark[v] = 0;
        }
    }
}

int main (){
    int T, k;
    scanf("%d", &T);
    while(T--){
        scanf("%d%d%d%d%d", &k ,&N, &E, &A, &B);
        init();
        getmap();
        maxs = 0;
        mark[A] = 1;
        DFSGET(A, INF);
        double num = maxflow(A, B) * 1.0 / maxs;
        printf("%d %.3lf\n", k, num);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值