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

题目大意:

给定n个点,m条有向边,每个点是一个吃饭的地方,每个人一盒饭。每个点有S个人,有B盒饭。每条边只能被走c次,每条边上都有电线,
第一个人通过的时候,不会破坏电线,从第二个人开始,每次都有概率p破坏掉电线。使得每个人都能吃饭,求最小破坏电线的概率。

解题思路:

题目要求我们求最小破坏电线的概率,就是一个最小乘积问题,加上log可以将其转变为加法,那样就可以使用费用刘来解决了。

按以下方式建图:

①源点st向第i个人建边,流量为S。

②第i个人向汇点建边,流量为B。

③u->v连边,流量为c,花费为-log(1-p)。

然后跑费用流,1-exp(-cost)即为答案。

代码

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#define LL long long
#define pii pair<double,int>
#define pll pair<long long,long long>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
#define bug cout<<"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"<<endl;
#define bugc(_) cout << (#_) << " = " << (_) << endl;
using namespace std;
const double eps=1e-;
const int N=1e2+;
const int M=1e5+;
const int INF=0x3f3f3f3f; struct node{
int to,next,flow;
double cost;
}edge[M*]; int cnt,st,en,n,m;
int head[N],pre[N];
double dis[N];//dis[i]表示到dis[i]为止不破坏电线的最大概率
bool vis[N]; int sgn(double x) { return x < -eps? -: x > eps; } void init(){
cnt=;
memset(head,,sizeof(head));
} void link(int u,int v,int flow,double cost){
edge[cnt]=node{v,head[u],flow,cost};
head[u]=cnt++;
edge[cnt]=node{u,head[v],,-cost};
head[v]=cnt++;
} bool spfa() {
memset(pre,,sizeof(pre));
memset(vis,false,sizeof(vis));
for(int i=st;i<=en;i++) dis[i]=INF;
dis[st]=;
queue<int>q;
q.push(st);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i;i=edge[i].next){
node t=edge[i];
if(t.flow&&dis[t.to]>dis[u]+t.cost+eps){
dis[t.to]=dis[u]+t.cost;
pre[t.to]=i;
if(!vis[t.to]){
vis[t.to]=true;
q.push(t.to);
}
}
}
}
if(dis[en]==INF)
return false;
return true;
} void mcmf(int &flow,double &cost){
while(spfa()){
int mmin=INF;
for(int i=pre[en];i;i=pre[edge[i^].to]){
mmin=min(mmin,edge[i].flow);
}
for(int i=pre[en];i;i=pre[edge[i^].to]){
edge[i].flow-=mmin;
edge[i^].flow+=mmin;
cost+=edge[i].cost*mmin;
}
flow+=mmin;
}
} int main(){
int T;
scanf("%d",&T);
while(T--){
init();
int n,m;
scanf("%d%d",&n,&m);
st=,en=n+;
for(int i=;i<=n;i++){
int s,b;
scanf("%d%d",&s,&b);
if(s-b>) link(st,i,s-b,);
if(s-b<) link(i,en,b-s,);
}
for(int i=;i<=m;i++){
int u,v,flow;
double p;
scanf("%d%d%d%lf",&u,&v,&flow,&p);
p=-log(-p);
if(flow>) link(u,v,,);
if(flow>) link(u,v,flow-,p);
}
int flow=;
double cost=;
mcmf(flow,cost);
cost=exp(-cost);
printf("%.2f\n",-cost);
}
return ;
}

HDU 5988 Coding Contest(费用流+浮点数)的更多相关文章

  1. HDU 5988 Coding Contest(浮点数费用流)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5988 题意:在acm比赛的时候有多个桌子,桌子与桌子之间都有线路相连,每个桌子上会有一些人和一些食物 ...

  2. HDU 5988.Coding Contest 最小费用最大流

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  3. HDU 5988 Coding Contest(最小费用最大流变形)

    Problem DescriptionA coding contest will be held in this university, in a huge playground. The whole ...

  4. Coding Contest(费用流变形题,double)

    Coding Contest http://acm.hdu.edu.cn/showproblem.php?pid=5988 Time Limit: 2000/1000 MS (Java/Others) ...

  5. hdu-5988 Coding Contest(费用流)

    题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot ...

  6. 2016青岛区域赛.Coding Contest(费用流 + 概率计算转换为加法计算)

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  7. HDU5988 - 2016icpc青岛 - G - Coding Contest 费用流(利用对数化乘为加

    HDU5988 题意: 有n个区域,每个区域有s个人,b份饭.现在告诉你每个区域间的有向路径,每条路有容量和损坏路径的概率.问如何走可以使得路径不被破坏的概率最小.第一个人走某条道路是百分百不会损坏道 ...

  8. HDU 5988 Coding Contest 最小费用流 cost->double

    Problem Description A coding contest will be held in this university, in a huge playground. The whol ...

  9. HDU5988 Coding Contest(费用流)

    2016青岛现场赛的一题,由于第一次走过不会产生影响,需要拆点,不过比赛时没想到,此外还有许多细节要注意,如要加eps,时间卡得较紧要注意细节优化等 #include <iostream> ...

随机推荐

  1. Django 设置media static

    ango 设置media static 本文python版本3.6.1,Django版本1.11.1 1.settings.py配置 增加django.template.context_process ...

  2. BootStrap行内编辑

    Bootstrap行内编辑,这里下载了一个X-Editable的插件,在Nuget里面就可以搜到. 引用的js和css大致如下: @*.Jquery组件引用*@ <script src=&quo ...

  3. Hive记录-加载文件进行查询操作

    Hive可以运行保存在文件里面的一条或多条的语句,只要用-f参数,一般情况下, 保存这些Hive查询语句的文件通常用.q或者.hql后缀名,但是这不是必须的, 你也可以保存你想要的后缀名.假设test ...

  4. WebApi中的参数传递

    在WebApi开发过程中,遇到一些客户端参数格式传输错误,经常被问到参数如何传递的一些问题,因此就用这篇博客做一下总结,肯定其它地方呢也有类似的一些文章,但是我还是喜欢通过这种方式将自己的理解记录下来 ...

  5. python学习笔记8-异常处理

    一.异常处理 在程序运行过程中,总会遇到各种各样的错误.程序一出错就停止运行了,那我们不能让程序停止运行吧,这时候就需要捕捉异常了,通过捕捉到的异常,我们再去做对应的处理. 下面我们先写一个函数,实现 ...

  6. KillerBee

    KillerBee介绍 KillerBee----是攻击zigbee和IEEE 802.15.4网络的框架和工具.使用killerBee工具和一个兼容的IEEE 802.15.4无线接口,你就能窃取z ...

  7. Docker入门02——Dockerfile详解

    基本示例 FROM MAINTAINER LABEL RUN ADD COPY CMD ENTRYPOINT ENV EXPOSE VOLUME WORKDIR USER ARG 基本示例 # Thi ...

  8. 多文档界面QMdiArea

    当使用多文档界面功能时,我们是将QMdiArea作为主窗口的中央部件,然后在这个中央部件中,我们可以同时打开很多个子窗口QMdiSubWindow 样式: import sys from PyQt5. ...

  9. JavaScript之JS单线程|事件循环|事件队列|执行栈

    本博文基于知乎"JavaScript作用域问题?"一问,而引起了对JavaScript事件循环和单线程等概念与实践上的研究.深入理解. 一.概念 0.关键词:JavaScript单 ...

  10. Biorhythms HDU - 1370 (中国剩余定理)

    孙子定理: 当前存在三个式子,t%3=2,t%5=3,t%7=2.然后让你求出t的值的一个通解. 具体过程:选取3和5的一个公倍数t1能够使得这个公倍数t1%7==1,然后选取3和7的一个公倍数t2使 ...