HDU5988 - 2016icpc青岛 - G - Coding Contest 费用流(利用对数化乘为加
题意:
有n个区域,每个区域有s个人,b份饭。现在告诉你每个区域间的有向路径,每条路有容量和损坏路径的概率。问如何走可以使得路径不被破坏的概率最小。第一个人走某条道路是百分百不会损坏道路的。
思路:
对于每个人,他从起点到目的地,不损坏道路的概率是(1 - p【1】*p【2】...*p【r】)。相乘不好做,对减号右边的乘法取对数,就成了相加,就可以愉快的做相加运算了,就是可以跑费用流了。这道题spfa还有加入esp=1e-8。
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;
typedef pair<pii,int> pp3;
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e8+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
template<typename T>inline void mod_(T &A,ll MOD=mod) {A%=MOD; A+=MOD; A%=MOD;} template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------show time----------------------*/
const int maxn = 1e6+;
struct edge{
int to,val,nxt;
double cost;
}gedge[maxn];
int h[maxn],gpre[maxn];
int gpath[maxn];
double gdist[maxn];
bool in[maxn];
int gcount = ,n,m; bool spfa(int s,int t){
//memset(gdist,inf,sizeof(gdist));
for(int i=; i<=n+; i++) {
gpre[i] = -;
gdist[i] = 999999999.9;
in[i] = false;
}
gdist[s] = 0.0; in[s] = true;
queue<int>que;
que.push(s);
while(!que.empty()){
int u = que.front();
que.pop(); in[u] =false;
for(int e = h[u]; e!=-; e= gedge[e].nxt){
int v = gedge[e].to;
double w = gedge[e].cost;
if(gedge[e].val > && gdist[v] - gdist[u] - w > 1e-){
gdist[v] = gdist[u] + w;
gpre[v] = u;
gpath[v] = e;
if(!in[v]){
que.push(v); in[v] = true;
}
}
}
} if(gpre[t] == -) return false;
return true;
} double MinCostFlow(int s,int t){
double cost = 0.0;int flow = ;
while(spfa(s,t)){
double f = 999999999.99;
for(int u=t; u!=s;u = gpre[u]){
if(gedge[gpath[u]].val < f){
f = gedge[gpath[u]].val;
}
}
flow += f;
cost += 1.0*gdist[t] * f;
for(int u=t; u!=s; u = gpre[u]){
gedge[gpath[u]].val -= f;
gedge[gpath[u] ^ ].val += f;
}
}
return cost;
} void addedge(int u,int v,int val, double cost){
gedge[gcount].to = v;
gedge[gcount].val = val;
gedge[gcount].cost = cost;
gedge[gcount].nxt = h[u];
h[u] = gcount++; gedge[gcount].to = u;
gedge[gcount].val = ;
gedge[gcount].cost = -cost;
gedge[gcount].nxt = h[v];
h[v] = gcount++;
}
int main(){
int T; scanf("%d", &T);
while(T--){
memset(h,-,sizeof(h)); gcount = ;
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++){
int u,v;
scanf("%d%d", &u, &v);
if(u > v) addedge(,i,u-v,0.0);
else if(u < v) addedge(i,n+,v-u,0.0);
}
for(int i=; i<=m; i++){
int u,v,c;double p;
scanf("%d%d%d%lf", &u, &v, &c, &p);
if(c == )continue;
addedge(u,v,,0.0);
addedge(u,v,c-,-1.0*log(-p));
}
double ans = -1.0*MinCostFlow(,n+); printf("%.2f\n", -exp(ans));
} return ;
}
HDU5988
HDU5988 - 2016icpc青岛 - G - Coding Contest 费用流(利用对数化乘为加的更多相关文章
- 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 ...
- 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(费用流+浮点数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5988 题目大意: 给定n个点,m条有向边,每个点是一个吃饭的地方,每个人一盒饭.每个点有S个人,有B盒 ...
- HDU5988 Coding Contest(费用流)
2016青岛现场赛的一题,由于第一次走过不会产生影响,需要拆点,不过比赛时没想到,此外还有许多细节要注意,如要加eps,时间卡得较紧要注意细节优化等 #include <iostream> ...
- hdu5988(费用流,对数相乘做加法)
题意:一个网络流的图,有n个点,从1~n,然后m条边,每个点有两个值,一个是人的数量si一个是饭的数量bi.每条m边有容量ci,还有走上去可能踩断电线的概率pi(第一次踩上去没有事,之后都要p概率). ...
- BZOJ 4276 [ONTAK2015]Bajtman i Okrągły Robin 费用流+线段树优化建图
Description 有n个强盗,其中第i个强盗会在[a[i],a[i]+1],[a[i]+1,a[i]+2],...,[b[i]-1,b[i]]这么多段长度为1时间中选出一个时间进行抢劫,并计划抢 ...
- Coding Contest HDU - 5988(费用流)
题意: 有n个区域和m条路,每个区域有a[i]个人和b[i]个食物,然后是m条路连接两个区域,这条路容量为cap,这条路断掉的概率为p,第一个经过的时候一定不会断,后面的人有概率p会断,现在需要所有人 ...
- HDU5988/nowcoder 207G - Coding Contest - [最小费用最大流]
题目链接:https://www.nowcoder.com/acm/contest/207/G 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5988 ...
随机推荐
- Djangou中使用cookie和session
一.会话跟踪 我们先需要了解是什么是会话!可以把会话理解为客户端与服务器之间的一次会话,在一次会话中可能会包含多次请求和响应,例如你给10086打个电话,你就是客户端,而10086服务人员就是服务器, ...
- Unity经典游戏教程之:贪吃蛇
版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...
- 不用 Spring Security 可否?试试这个小而美的安全框架
写在前面 在一款应用的整个生命周期,我们都会谈及该应用的数据安全问题.用户的合法性与数据的可见性是数据安全中非常重要的一部分.但是,一方面,不同的应用对于数据的合法性和可见性要求的维度与粒度都有所区别 ...
- feign传输String json串 自动转义 \ 解决方法
@RequestMapping(value={"/sysOrgRest/getInfoByOrgIds"}, method={org.springframework.web.bin ...
- MQ 服务器错误代码2035
MQ 服务器错误代码20352013-06-12 19:29:39 搭建一个MQ7.1服务器,用了一个小的demo测试程序,结果报错, 测试代码: import com.ibm.mq.MQC; imp ...
- Spring项目集成ShiroFilter简单实现权限管理
Shiros是我们开发中常用的用来实现权限控制的一种工具包,它主要有认证.授权.加密.会话管理.与Web集成.缓存等功能.我是从事javaweb工作的,我就经常遇到需要实现权限控制的项目,之前我们都是 ...
- python练习题-1
1.输出正方形 x=input("请输入:") x=int(x) for i in range(0,x): if (i==0) or (i==x-1): print("* ...
- python2.7官方文档阅读笔记
官方地址:https://docs.python.org/2.7/tutorial/index.html 本笔记只记录本人不熟悉的知识点 The Python Tutorial Index 1 Whe ...
- (十)c#Winform自定义控件-横向列表
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- SpringBoot内置tomcat启动原理
前言 不得不说SpringBoot的开发者是在为大众程序猿谋福利,把大家都惯成了懒汉,xml不配置了,连tomcat也懒的配置了,典型的一键启动系统,那么tomcat在springb ...