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(费用流)的更多相关文章

  1. hdu-5988 Coding Contest(费用流)

    题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot ...

  2. HDU5988 - 2016icpc青岛 - G - Coding Contest 费用流(利用对数化乘为加

    HDU5988 题意: 有n个区域,每个区域有s个人,b份饭.现在告诉你每个区域间的有向路径,每条路有容量和损坏路径的概率.问如何走可以使得路径不被破坏的概率最小.第一个人走某条道路是百分百不会损坏道 ...

  3. Coding Contest(费用流变形题,double)

    Coding Contest http://acm.hdu.edu.cn/showproblem.php?pid=5988 Time Limit: 2000/1000 MS (Java/Others) ...

  4. 2016青岛区域赛.Coding Contest(费用流 + 概率计算转换为加法计算)

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  5. HDU 5988 Coding Contest(费用流+浮点数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5988 题目大意: 给定n个点,m条有向边,每个点是一个吃饭的地方,每个人一盒饭.每个点有S个人,有B盒 ...

  6. 【费用流】hdu5988 Coding Contest

    从源点向每个点连接容量为该点人数,费用为1的边, 把原图中的每条边拆成两条,一条容量为1,费用为1,另一条容量为ci-1,费用为1-pi 从每个点向汇点连接容量为该点面包数量,费用为1的边. 跑的费用 ...

  7. hdu5988 Coding Contest

    首先这是个费用流,用log转乘法为加法,外加模板的修改,需加eps 下面是废话,最好别看 闲来无事(鼓起勇气)写这篇博客 这是个自带影像回访的题目 青岛的炼铜之旅,大学的acm生涯就这样结束了.或许还 ...

  8. HDU5988/nowcoder 207G - Coding Contest - [最小费用最大流]

    题目链接:https://www.nowcoder.com/acm/contest/207/G 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5988 ...

  9. HDU 5988.Coding Contest 最小费用最大流

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

随机推荐

  1. thinkphp5.0助手函数占用服务器资源

    db('user')  默认情况下,每次请求都会重新连接数据库,这样会占用服务器资源 方法1.如果不想每次都重连可以这样 db("List",[],false) 方法2.还可以直接 ...

  2. gevent

    gevent是一个基于协程的python网络库. 特性: 1.基于libev的事件循环 2.基于greenlet 轻量级的执行单元  (what is greenlet ?) 3.来自python标准 ...

  3. php如何防止图片盗用/盗链的两种方法(转)

    图片防盗链有什么用? 防止其它网站盗用你的图片,浪费你宝贵的流量.本文章向大家介绍php防止图片盗用/盗链的两种方法 Apache图片重定向方法 设置images目录不充许http访问 Apache服 ...

  4. 基于MQTT协议进行应用开发

    官方协议有句如下的话来形容MQTT的设计思想: "It is designed for connections with remote locations where a "sma ...

  5. .NET开源资源汇总

    1>>  力软信息化系统快速开发框架 2>>  金碟友商网 3>>

  6. shell脚本学习第一课

    shell是一种程序设计语言,是访问操作系统内核的服务. Linux的shell种类常见的有: Bourne Shell(/usr/bin/sh或/bin/sh) Bourne Again Shell ...

  7. 【学习笔记】JAva编程思想之多态

    1.如果java的基类拥有某个已被多次重载的方法名称,那么在导出类中重新定义该方法名称并不会屏蔽在基类的任何版本.因此,无论是在该层或者他的基类中对方法进行定义,重载机制都可以正常工作. 2.使用@O ...

  8. jq绑定事件的4种方式

    jQuery提供了多种绑定事件的方式,每种方式各有其特点,明白了它们之间的异同点,有助于我们在写代码的时候进行正确的选择,从而写出优雅而容易维护的代码.下面我们来看下jQuery中绑定事件的方式都有哪 ...

  9. hdu4405 Aeroplane chess

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  10. Bootstrap table使用心得

    序号显示带分页信息的连续编号,在序号列添加以下格式化代码即可. { field: 'number', title: '序号', align:'center', width:45, formatter: ...