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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章