HDU5988 Coding Contest(费用流)
2016青岛现场赛的一题,由于第一次走过不会产生影响,需要拆点,不过比赛时没想到,此外还有许多细节要注意,如要加eps,时间卡得较紧要注意细节优化等
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N = 108, M = 50000;
const int INF = 1e9;
const double eps = 1e-7;
int q[1000000];
struct Node{
int u, v, cap;
double cost;
int next;
}edge[M];//有向图,u到v的容量,费用
int tot;
int head[N], pre[N], path[N];
double dis[N];
bool inq[N]; void init(){
tot = 0;
memset(head, -1, sizeof(head));
} void add(int u, int v, int cap, double cost){
edge[tot].u = u;
edge[tot].v = v;
edge[tot].cap = cap;
edge[tot].cost = cost;
edge[tot].next = head[u];
head[u] = tot++;
edge[tot].u = v;
edge[tot].v = u;
edge[tot].cap = 0;
edge[tot].cost = -cost;
edge[tot].next = head[v];
head[v] = tot++;
} bool SPFA(int st, int des){
memset(inq, 0, sizeof(inq));
fill(dis, dis + des + 2, INF);
memset(pre, -1, sizeof(pre));
int front = 0, rear = 0;
q[rear++] = st;
dis[st] = 0;
inq[st] = true;
while(front < rear){
int u = q[front++]; inq[u] = false;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].v;
if(edge[i].cap > 0 && dis[v] > dis[u] + edge[i].cost + eps){
dis[v] = dis[u] + edge[i].cost;
pre[v] = u;
path[v] = i;
if(!inq[v]){
inq[v] = true;
q[rear++] = v;
}
}
}
}
return pre[des] != -1;
//return dis[des] + eps < INF;
} double EdmondsKarp(int st, int des){
double mincost = 0, flow = 0;
while(SPFA(st, des)){
int f = INF;
for(int i = des; i != st; i = pre[i]){
if(f > edge[path[i]].cap){
f = edge[path[i]].cap;
}
}
for(int i = des; i != st; i = pre[i]){
edge[path[i]].cap -= f;
edge[path[i]^1].cap += f;
}
mincost += f * dis[des];
flow += f;
}
return mincost;
}
int main(){
int t;
cin >>t;
while(t--){
int n, m;
scanf("%d %d", &n, &m);
init();
int st = 0, des = n + 1;
for(int i = 1; i <= n; i++){
int si, bi;
scanf("%d %d", &si, &bi);
if(si > bi){
add(st, i, si - bi, 0.0);
}else if(si < bi){
add(i, des, bi - si, 0.0);
}
}
while(m--){
int u, v, c;
double p;
scanf("%d %d %d %lf", &u, &v, &c, &p);
if(c > 0){
add(u, v, 1, 0);
if(c > 1){
add(u, v, c - 1, -log10(1 - p));
}
}
}
printf("%.2f\n", 1.0 - pow(10.0, -EdmondsKarp(st, des))); }
return 0;
}
HDU5988 Coding Contest(费用流)的更多相关文章
- hdu-5988 Coding Contest(费用流)
		题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ... 
- HDU5988 - 2016icpc青岛 - G - Coding Contest 费用流(利用对数化乘为加
		HDU5988 题意: 有n个区域,每个区域有s个人,b份饭.现在告诉你每个区域间的有向路径,每条路有容量和损坏路径的概率.问如何走可以使得路径不被破坏的概率最小.第一个人走某条道路是百分百不会损坏道 ... 
- Coding Contest(费用流变形题,double)
		Coding Contest http://acm.hdu.edu.cn/showproblem.php?pid=5988 Time Limit: 2000/1000 MS (Java/Others) ... 
- 2016青岛区域赛.Coding Contest(费用流 + 概率计算转换为加法计算)
		Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ... 
- HDU 5988 Coding Contest(费用流+浮点数)
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5988 题目大意: 给定n个点,m条有向边,每个点是一个吃饭的地方,每个人一盒饭.每个点有S个人,有B盒 ... 
- 【费用流】hdu5988 Coding Contest
		从源点向每个点连接容量为该点人数,费用为1的边, 把原图中的每条边拆成两条,一条容量为1,费用为1,另一条容量为ci-1,费用为1-pi 从每个点向汇点连接容量为该点面包数量,费用为1的边. 跑的费用 ... 
- hdu5988 Coding Contest
		首先这是个费用流,用log转乘法为加法,外加模板的修改,需加eps 下面是废话,最好别看 闲来无事(鼓起勇气)写这篇博客 这是个自带影像回访的题目 青岛的炼铜之旅,大学的acm生涯就这样结束了.或许还 ... 
- HDU5988/nowcoder 207G - Coding Contest - [最小费用最大流]
		题目链接:https://www.nowcoder.com/acm/contest/207/G 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5988 ... 
- HDU 5988.Coding Contest 最小费用最大流
		Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ... 
随机推荐
- redis 操作 list 的测试
			lpush 从头部压入数据 127.0.0.1:6379> lpush listname value1 (integer) 1//返回list的当前长度 127.0.0.1:6379> l ... 
- 解决nginx使用proxy_pass反向代理时,cookie丢失的问题
			1. 如果只是host.端口转换,则cookie不会丢失.例如: location /project { proxy_pass http://127.0.0.1:8080/pr ... 
- mysql二级索引
			以InnoDB来说,每个InnoDB表具有一个特殊的索引称为聚集索引.如果您的表上定义有主键,该主键索引是聚集索引.如果你不定义为您的表的主键 时,MySQL取第一个唯一索引(unique)而且只含非 ... 
- window.open打开新窗口被浏览器拦截的处理方法
			一般我们在打开页面的时候, 最常用的就是用<a>标签,如果是新窗口打开就价格target="_blank"属性就可以了, 如果只是刷新当前页面就用window.loca ... 
- web app开发利器 - iscroll4 解决方案
			存在即是道理,iscroll会诞生,主要是因为无论是在iphone.ipod.android 或是更早前的移动webkit都没有提供一种原生的方式来支持在一个固定高度的容器内滚动内容, 这个不幸的规则 ... 
- sql注入的基本防范手段
			基本的sql注入防御手段,概括来讲就是权限控制和关键词过滤. 防御sql注入 ============================================================= ... 
- Java开源库
			Java SE 7 API Docs from Oracle Apache IO库操作IO与文件 2.4 XML4j 1.6.1 Json.org google-json 2.5 WindowBuil ... 
- CentOS 6.5 安装 Redis-3.2.6
			到官网下载最新版的 Redis-3.2.6, 我把它放到文件夹:/usr/local/src/centos-sdk/source2/redis 安装脚本 redis-3.2.6.sh #!/bin/b ... 
- Virtualbox 上调整 Mac OS 分辨率 最简单方法
			Mac OS 分辨率:VBoxManage setextradata "Mac OS X 10.10" VBoxInternal2/EfiGopMode 3 ----代 ... 
- ListView控件--2016年12月9日
			ListView属性 ListView 名称 说明 AccessKey 重写 WebControl.AccessKey 属性. 不支持将此属性设置 ListView 控件.(覆盖 WebContr ... 
