题目链接: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 ;
}

最新文章

  1. iOS UITableView 与 UITableViewController
  2. c#-1 数据结构 定义相关 界面交互数据 Model层
  3. 移动端Viewport &amp; 使用rem来开发移动端网站
  4. Apache解析漏洞详解
  5. 二叉排序树(Binary Sort Tree)
  6. Validform 学习笔记---基础知识整理
  7. 生产者消费者模式--阻塞队列--LOCK,Condition--线程池
  8. poj 2536 Gopher II (二分匹配)
  9. Spring AOP 问与答
  10. 关于THIS_FILE
  11. POJ3660——Cow Contest(Floyd+传递闭包)
  12. Curl的编译
  13. javascript - 工作笔记 (事件绑定二)
  14. iOS面向对象的建模:MVC(OC基础)
  15. Linux 修改时区 不用重启
  16. Python数据分析工具
  17. linux安装ftp
  18. 第31月第25天 xcode debug 限制uitextfiled输入
  19. Mac OS环境下DOSBox汇编环境的搭建
  20. thinkphp 百度编辑器和layer简单用法

热门文章

  1. kali添加更新源
  2. 【Redis】DENIED Redis is running in protected mode
  3. 科学计算库Numpy——numpy.ndarray
  4. 让Python带你看一场唯美的横飘雪!
  5. 20181206(re,正则表达式,哈希)
  6. 03 Django视图
  7. stm32L0工程建立(HAL+IAR,无cubemx)
  8. angularjs报错问题记录
  9. Xampp 配置出现403无法访问
  10. JDK并发基础与部分源码解读