HDU4240 Route Redundancy

来源:互联网 发布:c语言入门之后学什么 编辑:程序博客网 时间:2024/06/06 00:27

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 0x7fffffffusing 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
原创粉丝点击