HDU - 5988 Coding Contest (最小費用最大流)

Coding Contest

 

A coding contest will be held in this university, in a huge playground. The whole playground would be divided into N blocks, and there would be M directed paths linking these blocks. The i-th path goes from the uiui-th block to the vivi-th block. Your task is to solve the lunch issue. According to the arrangement, there are sisi competitors in the i-th block. Limited to the size of table, bibi bags of lunch including breads, sausages and milk would be put in the i-th block. As a result, some competitors need to move to another block to access lunch. However, the playground is temporary, as a result there would be so many wires on the path.
For the i-th path, the wires have been stabilized at first and the first competitor who walker through it would not break the wires. Since then, however, when a person go through the i - th path, there is a chance of pipi to touch
the wires and affect the whole networks. Moreover, to protect these wires, no more than cici competitors are allowed to walk through the i-th path.
Now you need to find a way for all competitors to get their lunch, and minimize the possibility of network crashing.

Input

The first line of input contains an integer t which is the number of test cases. Then t test cases follow.
For each test case, the first line consists of two integers N (N ≤ 100) and M (M ≤ 5000). Each of the next N lines contains two integers si and bibi (sisi , bibi ≤ 200).
Each of the next M lines contains three integers uiui , vivi and ci(cici(ci ≤ 100) and a float-point number pipi(0 < pipi < 1).
It is guaranteed that there is at least one way to let every competitor has lunch.

Output

For each turn of each case, output the minimum possibility that the networks would break down. Round it to 2 digits.

Sample Input

1
4 4
2 0
0 3
3 0
0 3
1 2 5 0.5
3 2 5 0.5
1 4 5 0.5
3 4 5 0.5

Sample Output

0.50

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5988

題目大意:t組輸入,操場被分成n塊,有m條路,每一塊開始有ai個參賽人員,有bi份午飯, 每條路從ui到vi 最多能通過ci個人,每個人通過導致道路網絡癱瘓的概率是pi,其中第一個通過這條路的人不會導致道路癱瘓。問每個人都喫到午飯道路癱瘓的最小概率。

把概率取log,把乘法變成加法,然後就是最小費用最大流模板

代碼:

#include<bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
const int N=105;
const int M=50005;
const double eps=1e-7;
const int inf=0x3f3f3f3f;
double dis[N];
int pre[N],vis[N];
int maxflow,first[N],tot;
struct node
{
    int v,nex,cap,flow;
    double cost;
    node(){};
    node(int _v,int _c,int _nex,double _cost)
    {
        v=_v,cap=_c,nex=_nex,cost=_cost,flow=0;
    }
}e[M];
void init()
{
    mem(first,-1);
    tot=maxflow=0;
}
void add(int u,int v,int c,double cost)
{
    e[tot]=node(v,c,first[u],cost);
    first[u]=tot++;
    e[tot]=node(u,0,first[v],-cost);
    first[v]=tot++;
}
double spfa(int s,int t,int n)
{
    int i,u,v;
    queue<int>q;
    for(int i=1; i<=n; i++) vis[i]=0,dis[i]=inf,pre[i]=-1;
    vis[s]=1;
    dis[s]=0;
    q.push(s);
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=first[u]; i!=-1; i=e[i].nex)
        {
            v=e[i].v;
            if(e[i].cap>e[i].flow&&dis[v]>dis[u]+e[i].cost+eps)
            {
                dis[v]=dis[u]+e[i].cost;
                pre[v]=i;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    if(dis[t]==inf)
        return 0;
    return 1;
}
double MCMF(int s,int t,int n)
{
    int d;
    double mincost=0;
    while(spfa(s,t,n))
    {
        d=inf;
        for(int i=pre[t]; i!=-1; i=pre[e[i^1].v])
            d=min(d,e[i].cap-e[i].flow);
        maxflow+=d;
        for(int i=pre[t]; i!=-1; i=pre[e[i^1].v])
        {
            e[i].flow+=d;
            e[i^1].flow-=d;
        }
        mincost+=dis[t]*d;
    }
    return mincost;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        int n,m,x,y,w;
        double z;
        scanf("%d%d",&n,&m);
        int st=n+1,ed=n+2;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            add(st,i,x,0);
            add(i,ed,y,0);
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d%lf",&x,&y,&w,&z);
            z=-log2(1-z); 
            if(w) add(x,y,1,0);
            if(w>1) add(x,y,w-1,z);
        }
        double mincost=MCMF(st,ed,n+2);
        mincost=pow(2,-mincost);
        printf("%.2f\n",1-mincost);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章