HDU 5988最小网络流(浮点数)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5988
哇,以前的模版一直T,加了优先队列优化才擦边过。
建图很好建,概率乘法化成概率加法不会化。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = ;
const int INF = 1e9;
double dist[maxn];
int tot,head[maxn];
int pv[maxn],pe[maxn];
typedef pair<double,int> P;
double eps = 1e-;
struct edge
{
int to,pre,cap;
double cost;
}e[];
void init()
{
tot = ;
memset(head,-,sizeof(head));
}
void add(int from,int to,int cap,double cost)
{
e[tot].pre = head[from];
e[tot].to = to;
e[tot].cap = cap;
e[tot].cost = cost;
head[from] = tot++;
}
void addedge(int from,int to,int cap,double cost)
{
add(from,to,cap,cost);
add(to,from,,-cost);
}
int n;
double min_cost_flow(int s,int t,int f,int& max_flow)
{
double ret = 0.0;
while(f>)
{
priority_queue<P,vector<P>,greater<P> >q;
for(int i=;i<maxn;i++) dist[i] = INF;
dist[s] = 0.0;
q.push(P(,s));
while(!q.empty())
{
P cur = q.top(); q.pop();
int v = cur.second;
if(dist[v]<cur.first) continue;
for(int i=head[v];i>=;i=e[i].pre)
{
int to = e[i].to,cap = e[i].cap;
double cost = e[i].cost;
if(cap>&&(dist[to]-(dist[v]+cost))>=eps)
{
pv[to] = v,pe[to] = i;
dist[to] = dist[v] + cost;
q.push(P(dist[to],to));
}
}
}
if(fabs(dist[t]-INF)<=eps) return ret;///同一目的地,每次增广路都是最小费用
///当所有边的流量都流净后,即没有残余网络,返回。
int d = f;
for(int v=t;v!=s;v=pv[v])
{
d = min(d,e[pe[v]].cap);
}
f -= d;
max_flow += d;
ret += (double)d*dist[t]; ///走一单位就消耗dist[t]
for(int v=t;v!=s;v=pv[v])
{
e[pe[v]].cap -= d;
e[pe[v]^].cap += d;
}
}
return ret;
}
int main()
{
int T;scanf("%d",&T);
while(T--)
{
int m;
init();
scanf("%d %d",&n,&m);
int s = n+;
int t = n+;
for(int i=;i<=n;i++)
{
int si,bi;
scanf("%d %d",&si,&bi);
int x = si-bi;
if(x>) addedge(s,i,x,);
else if(x<) addedge(i,t,-x,);
}
int u,v,c;
double p;
for(int i=;i<=m;i++)
{
scanf("%d %d %d %lf",&u,&v,&c,&p);
p = -1.0*log2(1.0-p);
if(c>)
{
addedge(u,v,,);///第一个人不用费用
}
if(c->)
{
addedge(u,v,c-,p);
}
}
int max_flow = ;
double cost = min_cost_flow(s,t,INF,max_flow);
double di = 2.0;
printf("%.2f\n",(1.0-pow(di,-cost)));
}
return ;
}
HDU 5988最小网络流(浮点数)的更多相关文章
- HDU 3452 Bonsai(网络流之最小割)
题目地址:HDU 3452 最小割水题. 源点为根节点.再另设一汇点,汇点与叶子连边. 对叶子结点的推断是看度数是否为1. 代码例如以下: #include <iostream> #inc ...
- HDU 3046Pleasant sheep and big big wolf(切最小网络流)
职务地址:HDU 3046 最小割第一发!事实上也没什么发不发的. ..最小割==最大流.. 入门题,可是第一次入手最小割连入门题都全然没思路... sad..对最小割的本质还是了解的不太清楚.. 这 ...
- HDU 5988 Coding Contest(费用流+浮点数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5988 题目大意: 给定n个点,m条有向边,每个点是一个吃饭的地方,每个人一盒饭.每个点有S个人,有B盒 ...
- 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 ...
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- HDU 1533 最小费用最大流(模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void ...
- HDU 3374 最小/大表示法+KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给定一个串s,该串有strlen(s)个循环同构串,要求输出字典序最小的同构串的下标,字典 ...
随机推荐
- JAVA基础篇—文件上传下载
/index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...
- Mysql主键一致时,可以进行在元数据上的操作
insert into daliy_hit_counter(day,slot,cnt) values(12,12,1) on duplicate key update cnt = cnt +1 dal ...
- Linux学习-用 make 进行宏编译
为什么要用 make 先来想象一个案例,假设我的执行档里面包含了四个原始码文件,分别是 main.c haha.c sin_value.c cos_value.c 这四个文件,这四个文件的目的是: m ...
- /dev/sda is apparently in use by the system; will not make a filesystem here!解决方法
/dev/sda is apparently in use by the system; will not make a filesystem here! 翻译:系统显然在使用,不会在这里做文件系统 ...
- HDU4010 Query on The Trees (LCT动态树)
Query on The Trees Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Othe ...
- 26、android上跑apache的ftp服务
一.为啥 在android设备跑ftp服务,在现场方便查看日志,目前就是这么用的. 二.前提: 从apache的官网下载依赖包:http://mina.apache.org/ftpserver-pro ...
- 用PHP写的一个简单的分页类 2.0版
<?php /* 分页类 用于实现对多条数据分页显示 version:2.0 //基于1.0 数据库查询用mysqli实现 author:Knight E-Mail:S.Knight.Work@ ...
- python集合、字符编码、bytes与二进制
集合 用括号表示{ },可以包含多个元素,用逗号分割 用途 用于关系运算 集合特点 1.每个元素是不可变类型 2.没有重复的元素 3.无序 应用 1.set去重 set(names)的功能是将列表转换 ...
- TOJ1698: Balanced Lineup
Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...
- “玲珑杯”ACM比赛 Round #23
A -- 生是脂肪的人 Time Limit:2s Memory Limit:128MByte Submissions:263Solved:97 DESCRIPTION 给定一个整数n,输出[(10^ ...