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

HDU - 5988 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 ui-th block to the vi-th block. Your task is to solve the lunch issue. According to the arrangement, there are si competitors in the i-th block. Limited to the size of table, bi 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 pi to touch
the wires and affect the whole networks. Moreover, to protect these wires, no more than ci 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 bi (si , bi ≤ 200).
Each of the next M lines contains three integers ui , vi and ci(ci ≤ 100) and a float-point number pi(0 < pi < 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

題目大意:

有一個遊樂場被分成了n個點。每個點都有x人。和y個食物。
現在要讓每個人都有食物喫。就必定要有某些點的人到別的點上去。
然後有m條邊,每條邊都有一個最大流量f,表示這個邊最多被f個人經過。並且經過這條邊都有p的可能性會撞到電線杆。(第一次經過不可能撞到電線杆)
問,讓所有人都有食物喫,並且要最小化撞到電線杆的可能性。

解題思路:

把食物不夠的點和源點連,流量是還差多少食物。花費是0
把食物多的點和匯點連,流量是多了多少食物,花費是0
然後給定了m條邊,流量就是那個最大流量
假設給定一個 u,v,w,p
這個這兩邊經過兩次 被破壞的可能性是pp 三次是pp*p 但是網絡流只能解決和的問題。所以我們想辦法把他變成和的形式,那就加一個log2 這樣乘積就變成了和的形式。但是這樣算出來的最低可能性取log就是個負數,所以我們求1-p的倒數然後取log。這樣就可以建圖跑費用流了
add(u,v,1,0)/第一次撞不可能撞到
add(u,v,flow-1.-log(1-p));

AC代碼:

#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f;
using namespace std;
const double eps = 1e-7;
const int maxn = 400;
const int maxm = 4e4+10;
int head[maxn];
struct node{
    int to;
    int cap;
    double cost;
    int flow;
    int next;
}side[maxm];
int n,m;
int cnt = 0;
int vis[maxn];
double dis[maxn];
int pre_point[maxn];//前驅結點
int pre_side[maxn];
void init(){
    memset(head,-1,sizeof(head));
    cnt = 0;
}
void add(int x,int y,int cap,double cost){
    side[cnt].to = y;
    side[cnt].cap = cap;
    side[cnt].cost = cost;
    side[cnt].next = head[x];
    side[cnt].flow = 0;
    head[x] = cnt++;

    side[cnt].to = x;
    side[cnt].flow = 0;
    side[cnt].cap = 0;//反向流量初始爲0
    side[cnt].cost = -cost;//反向花費爲負值
    side[cnt].next = head[y];
    head[y] = cnt++;
}
bool spfa(int s,int e){
    memset(pre_side,-1,sizeof(pre_side));
    memset(pre_point,-1,sizeof(pre_point));
    for(int i=0;i<maxn;i++){
    	dis[i] = inf*1.0;
	}
    memset(vis,0,sizeof(vis));
    dis[s] = 0;
    vis[s] = 1;
    queue<int> q;
    q.push(s);
    while(q.size()){
        int now = q.front();
        q.pop();
        vis[now] = 0;
        for(int i=head[now];i!=-1;i=side[i].next){
            if(side[i].cap>side[i].flow){
                int to = side[i].to;
                if(dis[to]>dis[now]+side[i].cost+eps){
                    dis[to] = dis[now]+side[i].cost;
                    pre_side[to] = i;
                    pre_point[to] = now;
                    if(!vis[to]){
                        q.push(to);
                        vis[to] = 1;
                    }
                }
            }
        }
    }
    if(dis[e]==inf){//沒有找到增廣路
		return false;
	}else return true;
}
double min_cost_flow(int s,int e){
    int ans_flow = 0;
    double ans_cost = 0;
    int u,mn;
    while(spfa(s,e)){
        u = e;
        int f = 0x3f3f3f3f;
        for(int u=pre_side[e];u!=-1;u=pre_side[side[u^1].to]){
            if(f>side[u].cap-side[u].flow){
                f= side[u].cap-side[u].flow;
            }
            ans_flow +=f;
        }
       for(int i=pre_side[e];i!=-1;i=pre_side[side[i^1].to]){
            side[i].flow+=f;
            side[i^1].flow -=f;
            ans_cost+=side[i].cost*f;
        }
    }
    return ans_cost;
}
int main(){
    int t;
   	scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        init();
        int s = 0;
        int e = n+1;
        for(int i=1;i<=n;i++){
        	int num,hava;
            scanf("%d%d",&num,&hava);
            if(num>hava){
                add(s,i,num-hava,0);
            }else if(hava>num){
                add(i,e,hava-num,0);
            }
        }
        while(m--){
            int x,y,f;
            double p;
            scanf("%d%d%d%lf",&x,&y,&f,&p);
            p = -log2(1-p);//最大化不踩線的負數
            add(x,y,1,0);
		    add(x,y,f-1,p);
        }
        double ans=min_cost_flow(s,e);
        ans = -ans;
        printf("%.2f\n",1-pow(2,ans));
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章