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> ...
随机推荐
- 面向对象【day07】:类的实例化过程剖析(三)
本节内容 1.概述 2.类的语法 3.总结 一.概述 之前我们说关于python中的类,都一脸懵逼,都想说,类这么牛逼到底是什么,什么才是类?下面我们就来讲讲,什么是类?它具有哪些特性. 二.类的语法 ...
- java8的新特性详解-----------Lamda表达式
java8最大的亮点就是引入了Lamda表达式 , 函数式编程的概念 具体啥意思我也不知道.只管用就行了,非常的强大,简洁,一个表达式相当于以前的十几行代码 因为之前要实现这种效果全靠if el ...
- 获取当前操作系统的ip
代码如下: #include "stdafx.h" #include <WinSock2.h> int get_local_ip() { WSADATA wsaData ...
- javascript 常用的正则表达式验证表单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MySQL 获得当前日期时间 函数【转】
获得当前日期+时间(date + time)函数:now() mysql> select now(); +---------------------+ | now() | +---------- ...
- 51NOD-1486 大大走格子
有一个h行w列的棋盘,里面有一些格子是不能走的,现在要求从左上角走到右下角的方案数. Input 单组测试数据. 第一行有三个整数h, w, n(1 ≤ h, w ≤ 10^5, 1 ≤ n ≤ 20 ...
- Simple Sort
题目描述 You are given an unsorted array of integer numbers. Your task is to sort this array and kill po ...
- 四、移植 JZ2440 开发板
4.1 移植第一步 前面已经分析过了 .config 的过程,可以知道移植需要用到的文件: .config 文件 arch/arm/cpu 下的文件 board 目录 .config 文件是根据后面 ...
- android 简单文件操作
1.布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...
- MacOs -bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
1解决iterm远程登录主机报错 -bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or ...