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 ...
随机推荐
- ITShare
框架底层通过IBatis和XML实现ORM映射: 业务处理类似UI,通过Controller和JavaScript交互: 视图通过Castle与NVelocity实现,jquery.tmpl渲染: . ...
- 【09-27】Spring 学习笔记
SpringMVC 配置全局异常处理器 import javax.servlet.http.HttpServletResponse; import org.springframework.web.bi ...
- 浅谈Java中的equals和==(转)
浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str ...
- [cocos] ( 01 ) cocos2d-x 3.x 开发 环境配置
有几个需要注意的问题 Windows上使用时, Unable to execute dex: Multiple dex files define 在eclipse中libcoco2dx的Java Bu ...
- 【Android】Android属性allowBackup安全风险
allowBackup值为true或false,Android API Level 8及其以上Android系统提供了为应用程序数据的备份和恢复功能,当allowBackup标志为true时,用户即可 ...
- basic use of sidekiq (2)
vim Gemfile source "https://rubygems.org" gem "sidekiq"gem 'rack-protection' gem ...
- maven向本地仓库导入jar包(处理官网没有的jar包)
对于官网没有的jar包,maven向本地仓库导入jar包用如下命令 mvn install:install-file -DgroupId=包名 -DartifactId=项目名 -Dversion=版 ...
- 耿丹CS16-2班第六次作业汇总
Deadline: 2016-11-13 11:59 作业内容 第六次作业总结 00.本次题目分值最高为**6分/题 × 7题 + 5分/篇 × 1篇 = 47分**,其中有新解法者每题加原创分**2 ...
- 3篇NeuroImage文献分析
鉴于之前读的一些文章很容易就忘掉了,故打算花点时间记录下所读的文献. 这几天花了一些时间读了3篇文献: Intersubject consistency of cortical MEG signals ...
- python2.7 报错(UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128))
报错: 原来用的python3.5版本后来改为2.7出现了这个错误里面的中文无法显示 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 ...