HDU4240 Route Redundancy

HDU4240 Route Redundancy
Background
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

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#define Inf 0x7fffffff
using namespace std;
int P,D,n,m,st,ed;
int len;
struct node
{
    int w,next,v;
}t[1000010];
int temp[1000010];
int head[1010],Maxd,pre[1010],dis[1010],gap[1010],cur[1010];


void add(int x,int y,int w)
{
    t[len].v=y;
    t[len].next=head[x];
    t[len].w=w;
    temp[len]=w;
    head[x]=len++;
}

int Isap()
{
    int flow=0,d=Inf,i,u;
    for (i=0; i<=n; i++)
    {
        cur[i]=head[i];
        dis[i]=gap[i]=0;
    }
    gap[0]=n;
    u=pre[st]=st;
    int flag;
    while (dis[u]<n)
    {
        flag=0;
        for (int &j=cur[u]; j!=-1; j=t[j].next)
        {
            //printf("%d\n",u);
            int v=t[j].v;
            if (t[j].w>0&&dis[v]+1==dis[u])
            {
                flag=true;
                d=min(d,t[j].w);
                pre[v]=u;
                u=v;
                if (u==ed)
                {
                    flow+=d;
                    int Mind=Inf;
                    bool mark=true;
                    while (u!=st)
                    {
                        u=pre[u];
                        if (temp[cur[u]]==0)  mark=false;
                        Mind=min(temp[cur[u]],Mind);
                        t[cur[u]].w-=d;
                        t[cur[u]^1].w+=d;
                    }
                    //printf("%d\n",Mind);
                    if(mark) Maxd=max(Mind,Maxd);
                    d=Inf;
                }
            break;
            }
        }
        if (flag) continue;
        int minh=n;
        for (int j=head[u]; j!=-1; j=t[j].next)
        {
            int v=t[j].v;
            if (t[j].w>0 && dis[v]<minh)
            {
                minh=dis[v];
                cur[u]=j;
            }
        }
        if ((--gap[dis[u]])==0)  break;
        dis[u]=minh+1;
        gap[dis[u]]++;
        u=pre[u];
    }
    return flow;
}


int main()
{
    freopen("1.in","r",stdin);
    int i,j,a,b,w;
    scanf("%d",&P);
    while (P--)
    {
        memset(head,-1,sizeof(head));
        scanf("%d",&D);
        scanf("%d%d%d%d",&n,&m,&st,&ed);
        len=0;
        for (i=0; i<m; i++)
        {
            scanf("%d%d%d",&a,&b,&w);
            add(a,b,w);
            add(b,a,0);
            //printf("%d %d %d\n",a,b,w);
        }
        Maxd=-1;
        double ans=1.0*Isap();
        //printf("%d \n",Maxd);
        ans=ans/Maxd;
        printf("%d %.3lf\n",D,ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值