HDU5988Coding Contest 【费用流】

Coding Contest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1199    Accepted Submission(s): 225


Problem Description
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


Source
2016ACM/ICPC亚洲区青岛站-重现赛(感谢中国石油大学)

明显的最小费用流问题
计算最小费用需要变化一下
显然 最小的崩溃概率=1-最大的不崩溃效率
对(1-p)取log 将乘法转化为加法即可直接求最大费用最大流

998ms卡过…..

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include <string.h>
#include<math.h>

using namespace std;
#define ll long long
#define pii pair<int,int>

const int INF = 1e9+7;

const double EPS = 1e-7;

const int N = 100 + 50;//点
const int M = 4*5000+50 + 2*N;//边

struct Edge{
    int to,c;
    double w;
    int next;//c:流量,w:费用
}edge[M];
int head[N];

int nume=0;
inline void addEdge(int k,int u,int v,int c,double w){//w:可能触碰到电线的概率
    edge[k].to=v,
            edge[k].c=c,
            edge[k].w=w,
            edge[k].next=head[u];
    head[u]=k;
}

inline void addEdge(int u,int v,int c,double w){
    addEdge(nume++,u,v,c,w);
    addEdge(nume++,v,u,0,-w);
}

void init(int n){
    fill(head,head+n+1,-1);
    nume=0;
}

bool used[N];
double dis[N];
int load[N],p[N];//距离 ,前驱边,前驱点
bool spfa(int s,int e,int n){
    deque<int>que;
    for(int i=0;i<=n;++i){
        dis[i]=INF;
        load[i]=p[i]=-1;
        used[i]=false;
    }
    que.push_back(s);
    dis[s]=0;
    used[s]=true;
    while(!que.empty()){
        int u=que.front();
        que.pop_front();
        used[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next){
            if(edge[i].c>0){
                int v=edge[i].to;
                if(dis[v]-EPS>dis[u]+edge[i].w){
                    dis[v]=dis[u]+edge[i].w;
                    p[v]=u;
                    load[v]=i;
                    if(used[v]==false){
                        used[v]=true;
                        que.push_back(v);
                    }
                }
            }
        }
    }
    return dis[e]!=INF;
}
double min_cost_flow(int s,int t,int n){
    int ansflow=0;//最大流
    double anscost=0;//最小费用
    while(spfa(s,t,n)){
        int u=t;
        int f=INF;
        while(p[u]!=-1){
            f=min(f,edge[load[u]].c);
            u=p[u];
        }
        u=t;
        while(p[u]!=-1){
            edge[load[u]].c-=f;
            edge[load[u]^1].c+=f;
            u=p[u];
        }
        anscost+=dis[t]*f;
        ansflow+=f;
    }
    return anscost;
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--){
        int n,m;
        scanf("%d%d",&n,&m);
        init(n+2);//S=n+1,T=n+2
        int s,b;
        for(int i=1;i<=n;++i){
            scanf("%d%d",&s,&b);
            addEdge(n+1,i,s,0);//起点到i
            addEdge(i,n+2,b,0);//i到终点
        }
        int u,v,c;
        double p;
        for(int i=1;i<=m;++i){
            scanf("%d%d%d%lf",&u,&v,&c,&p);
            addEdge(u,v,1,0);
            addEdge(u,v,c-1,-log(1-p));
        }
        double ans = min_cost_flow(n+1,n+2,n+2);
        printf("%.2f\n",1-exp(-ans));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值