hdu5988 Coding Contest

本文探讨了一种在编码竞赛中确保网络稳定性的算法。通过将概率乘法转换为对数加法并使用最小费用流算法,解决了选手在有限路径上移动以获取午餐时可能触发的网络崩溃风险问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.elijahqi.win/2018/01/10/hdu5988-coding-contest/
Coding Contest

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4164 Accepted Submission(s): 974
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亚洲区青岛站-重现赛(感谢中国石油大学)

这题非常的强orz 如何把乘法运算变成加法然后跑费用流 没错针对这个分数取一下log即可 但是问题在于这都是负数啊 我跑费用流 汇出现负权圈会有问题 那怎么办 我要求碰电线的概率最小是不是就是不碰电线的概率最大 所以我可以改为求-log(1-p)取一下对数 跑最小费用最大流即可 最后计算答案的时候由于都是对数 我要求的是总的概率最小 所以我直接把这些数加起来得到这个费用 然后再exp 求得e^x=ans 得到x
最后我的答案因为我预处理的时候是1-p所以 我需要输出1-x 这题可能我太菜了 竟然要卡常 spfa需要写slf优化 这个eps是看网上的人都这么写的 不知道如何计算这个eps gg


#include<cmath>
#include<deque>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define eps 1e-8
#define N 110
#define M 22000
#define inf 0x3f3f3f3f
using namespace std;
inline int read(){
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=getchar();
    return x;
}
struct node{
    int y,z,next;double c;
}data[M];
int num=1,flag[N],pre[N],path[N],h[N],T,TT,n,m;double f[N];
inline void insert1(int x,int y,int z,double c){
    data[++num].y=y;data[num].z=z;data[num].c=c;data[num].next=h[x];h[x]=num;
    data[++num].y=x;data[num].z=0;data[num].c=-c;data[num].next=h[y];h[y]=num;
}
inline bool spfa(){
    memset(f,127,sizeof(f));deque<int>q;//memset(flag,0,sizeof(flag));
    flag[0]=1;f[0]=0;memset(pre,-1,sizeof(pre));q.push_back(0);
    while(!q.empty()){
        int x=q.front();q.pop_front();flag[x]=0;
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y,z=data[i].z;double c=data[i].c;
            if(eps<f[y]-(f[x]+c)&&z){
                f[y]=f[x]+c;pre[y]=x;path[y]=i;
                if (!flag[y]) {
                    if (!q.empty()&&eps<f[q.front()]-f[y]) q.push_front(y);else q.push_back(y);flag[y]=1;
                }
            }
        }
    }if (pre[T]==-1) return 0;else return 1;
}
int main(){
    //freopen("hdu5988.in","r",stdin);
    TT=read();
    for (int jk=1;jk<=min(10,TT);++jk){


        n=read();m=read();memset(h,0,sizeof(h));num=1;T=n+1;
        for (int i=1;i<=n;++i) {
            int s=read(),b=read();if (s-b>0) insert1(0,i,s-b,0);else insert1(i,T,b-s,0);
        }
        for (int i=1;i<=m;++i){
            int x=read(),y=read(),z=read();double p;scanf("%lf",&p);
            insert1(x,y,z-1,-log(1-p));insert1(x,y,1,0);
        }double ans=0;
        while(spfa()){
            int minn=inf,now=T;
            while(now) minn=min(minn,data[path[now]].z),now=pre[now];now=T;ans+=f[T]*minn;
            while(now) {data[path[now]].z-=minn;data[path[now]^1].z+=minn;now=pre[now];}
        }printf("%.2lf\n",1.0-exp(-ans));
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值