HDU 5988最小网络流(浮点数)
2024-08-29 09:28:40
题目链接: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 ;
}
最新文章
- iOS UITableView 与 UITableViewController
- c#-1 数据结构 定义相关 界面交互数据 Model层
- 移动端Viewport &; 使用rem来开发移动端网站
- Apache解析漏洞详解
- 二叉排序树(Binary Sort Tree)
- Validform 学习笔记---基础知识整理
- 生产者消费者模式--阻塞队列--LOCK,Condition--线程池
- poj 2536 Gopher II (二分匹配)
- Spring AOP 问与答
- 关于THIS_FILE
- POJ3660——Cow Contest(Floyd+传递闭包)
- Curl的编译
- javascript - 工作笔记 (事件绑定二)
- iOS面向对象的建模:MVC(OC基础)
- Linux 修改时区 不用重启
- Python数据分析工具
- linux安装ftp
- 第31月第25天 xcode debug 限制uitextfiled输入
- Mac OS环境下DOSBox汇编环境的搭建
- thinkphp 百度编辑器和layer简单用法