HDU 5988 Coding Contest(费用流+浮点数)
题目链接: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(费用流+浮点数)的更多相关文章
- HDU 5988 Coding Contest(浮点数费用流)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5988 题意:在acm比赛的时候有多个桌子,桌子与桌子之间都有线路相连,每个桌子上会有一些人和一些食物 ...
- HDU 5988.Coding Contest 最小费用最大流
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- HDU 5988 Coding Contest(最小费用最大流变形)
Problem DescriptionA coding contest will be held in this university, in a huge playground. The whole ...
- Coding Contest(费用流变形题,double)
Coding Contest http://acm.hdu.edu.cn/showproblem.php?pid=5988 Time Limit: 2000/1000 MS (Java/Others) ...
- hdu-5988 Coding Contest(费用流)
题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- 2016青岛区域赛.Coding Contest(费用流 + 概率计算转换为加法计算)
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- HDU5988 - 2016icpc青岛 - G - Coding Contest 费用流(利用对数化乘为加
HDU5988 题意: 有n个区域,每个区域有s个人,b份饭.现在告诉你每个区域间的有向路径,每条路有容量和损坏路径的概率.问如何走可以使得路径不被破坏的概率最小.第一个人走某条道路是百分百不会损坏道 ...
- HDU 5988 Coding Contest 最小费用流 cost->double
Problem Description A coding contest will be held in this university, in a huge playground. The whol ...
- HDU5988 Coding Contest(费用流)
2016青岛现场赛的一题,由于第一次走过不会产生影响,需要拆点,不过比赛时没想到,此外还有许多细节要注意,如要加eps,时间卡得较紧要注意细节优化等 #include <iostream> ...
随机推荐
- MySQL_select语句(不定时更新)
1.SELECT语句 select if(fraction>=60 and fraction<=100,'合格','不合格') from sp_employeezzvalidate;
- nginx upstream的配置
upstream backend { server 13.4.2.14:8080 max_fails=2 fail_timeout=30s ; server 13.4.2.15:8080 max_fa ...
- WinForm ListView虚拟模式加载数据 提高加载速度
将VirtualMode 属性设置为 true 会将 ListView 置于虚拟模式.控件不再使用Collection.Add()这种方式来添加数据,取而代之的是使用RetrieveVirtualIt ...
- 多线程ExecutorService中submit和execute区别
submit和execute都是 ExecutorService 的方法,都是添加线程到线程池中. 区别 三个区别: 1.接收的参数不一样 2.submit有返回值,而execute没有 Method ...
- URL基本结构
先来简单说下URI.URL.URN这三个鬼东西. URI全称Uniform Resource Identifier,统一资源标识符 URL全称Uniform Resource Locator,统一资源 ...
- Json Schema简介
1. 引言 什么是Json Schema? 以一个例子来说明 假设有一个web api,接受一个json请求,返回某个用户在某个城市关系最近的若干个好友.一个请求的例子如下: { "city ...
- JavaScript之DOM概念
一.DOM概念 1.DOM是什么? 1.1 起源.DOM起源于Netscape与Microsoft 公司的DHTML(动态HTML). 1.2 名词解释.Document Object Model,文 ...
- Flask最强攻略 - 跟DragonFire学Flask - 第八篇 实例化Flask的参数 及 对app的配置
Flask 是一个非常灵活且短小精干的web框架 , 那么灵活性从什么地方体现呢? 有一个神奇的东西叫 Flask配置 , 这个东西怎么用呢? 它能给我们带来怎么样的方便呢? 首先展示一下: from ...
- Latex 公式居中
这么简单的功能要是还要加工具包,LaTeX也不用混了~ 公式用\[...\]来写可以达到公式居中效果. 或者在equation环境外加center环境: \begin{center} \begin{e ...
- ubuntu14.04 提示 卷 文件系统根目录 仅剩余xxx的硬盘空间