Coding Contest

http://acm.hdu.edu.cn/showproblem.php?pid=5988

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5337    Accepted Submission(s): 1256

Problem Description
A coding contest will be held in this university, in a huge playground. The whole playground would be divided into N blocks, and there would be M directed paths linking these blocks. The i-th path goes from the ui-th block to the vi-th block. Your task is to solve the lunch issue. According to the arrangement, there are sicompetitors in the i-th block. Limited to the size of table, bi bags of lunch including breads, sausages and milk would be put in the i-th block. As a result, some competitors need to move to another block to access lunch. However, the playground is temporary, as a result there would be so many wires on the path.
For the i-th path, the wires have been stabilized at first and the first competitor who walker through it would not break the wires. Since then, however, when a person go through the i - th path, there is a chance of pi to touch
the wires and affect the whole networks. Moreover, to protect these wires, no more than ci competitors are allowed to walk through the i-th path.
Now you need to find a way for all competitors to get their lunch, and minimize the possibility of network crashing.
 
Input
The first line of input contains an integer t which is the number of test cases. Then t test cases follow.
For each test case, the first line consists of two integers N (N ≤ 100) and M (M ≤ 5000). Each of the next N lines contains two integers si and bi (si , bi ≤ 200).
Each of the next M lines contains three integers ui , vi and ci(ci ≤ 100) and a float-point number pi(0 < pi < 1).
It is guaranteed that there is at least one way to let every competitor has lunch.
 
Output
For each turn of each case, output the minimum possibility that the networks would break down. Round it to 2 digits.
 
Sample Input
1
4 4
2 0
0 3
3 0
0 3
1 2 5 0.5
3 2 5 0.5
1 4 5 0.5
3 4 5 0.5
 
Sample Output
0.50
 
Source
 
求网络被破坏的最小可能性,因为是乘法,所以要用取对数的方法把它改成加法。
因为概率是小于1的,所以取对数完是负数,需要用 - 把它转为正数。
但是转为正数后,原来的最小值就会变为最大值,所以用p=-log2(1-p),转为求不被破坏的最大可能行,跑费用流
因为是浮点型,所以在松弛的时候要加上eps。
最后,要用for去代替memset,不然可能会t...
 
 #include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std; const double eps=1e-;
const int INF=0x3f3f3f3f;
const int N=;
const int M=;
int top;
double dist[N];
int pre[N];
bool vis[N];
int c[N];
int maxflow; struct Vertex{
int first;
}V[N];
struct Edge{
int v,next;
int cap,flow;
double cost;
}E[M]; void init(int num){
// memset(V,-1,sizeof(V));
for(int i=;i<num;i++){
V[i].first=-;
}
top=;
maxflow=;
} void add_edge(int u,int v,int c,double cost){
E[top].v=v;
E[top].cap=c;
E[top].flow=;
E[top].cost=cost;
E[top].next=V[u].first;
V[u].first=top++;
} void add(int u,int v,int c,double cost){
add_edge(u,v,c,cost);
add_edge(v,u,,-cost);
} bool SPFA(int s,int t,int n){
int i,u,v;
queue<int>qu;
// memset(vis,false,sizeof(vis));
// memset(c,0,sizeof(c));
// memset(pre,-1,sizeof(pre));
for(i=;i<=n+;i++){
dist[i]=INF;
vis[i]=false;
c[i]=;
pre[i]=-;
}
// memset(dist,INF,sizeof(dist));
vis[s]=true;
c[s]++;
dist[s]=;
qu.push(s);
while(!qu.empty()){
u=qu.front();
qu.pop();
vis[u]=false;
for(i=V[u].first;~i;i=E[i].next){
v=E[i].v;
if(E[i].cap>E[i].flow&&dist[v]>dist[u]+E[i].cost+eps){
dist[v]=dist[u]+E[i].cost;
pre[v]=i;
if(!vis[v]){
c[v]++;
qu.push(v);
vis[v]=true;
if(c[v]>n){
return false;
}
}
}
}
}
if(dist[t]==INF){
return false;
}
return true;
} double MCMF(int s,int t,int n){
int d,i;
double mincost=;
while(SPFA(s,t,n)){
d=INF;
for(i=pre[t];~i;i=pre[E[i^].v]){
d=min(d,E[i].cap-E[i].flow);
}
maxflow+=d;
for(i=pre[t];~i;i=pre[E[i^].v]){
E[i].flow+=d;
E[i^].flow-=d;
}
mincost+=dist[t]*d;
}
return mincost;
} int main(){
int n,m;
int T;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
init(n+);
int a,b,c;
double p;
int s=,t=n+;
for(int i=;i<=n;i++){
scanf("%d %d",&a,&b);
if(a>b){
add(s,i,a-b,);
}
else if(a<b){
add(i,t,b-a,);
}
}
for(int i=;i<=m;i++){
scanf("%d %d %d %lf",&a,&b,&c,&p);
if(c>) add(a,b,,);
if(c>) add(a,b,c-,-log2(-p));
}
double ans=MCMF(s,t,n+);
printf("%.2f\n",1.0-pow(,-ans));
}
}

Coding Contest(费用流变形题,double)的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU5988 Coding Contest(费用流)

    2016青岛现场赛的一题,由于第一次走过不会产生影响,需要拆点,不过比赛时没想到,此外还有许多细节要注意,如要加eps,时间卡得较紧要注意细节优化等 #include <iostream> ...

  6. Lunch Time(费用流变型题,以时间为费用)

    Lunch Time http://acm.hdu.edu.cn/showproblem.php?pid=4807 Time Limit: 4000/2000 MS (Java/Others)     ...

  7. HDU 3376 &amp;&amp; 2686 方格取数 最大和 费用流裸题

    题意: 1.一个人从[1,1] ->[n,n] ->[1,1] 2.仅仅能走最短路 3.走过的点不能再走 问最大和. 对每一个点拆点限流为1就可以满足3. 费用流流量为2满足1 最大费用流 ...

  8. POJ 3686 The Windy's(思维+费用流好题)

    The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5362   Accepted: 2249 Descr ...

  9. Going Home POJ - 2195 费用流板子题

    On a grid map there are n little men and n houses. In each unit time, every little man can move one ...

随机推荐

  1. Shell的for和select

    环境准备: [root@nodchen-db01-test day07]# mkdir -p /server/scripts/day07/test/{test.txt,oldboy.txt,oldgi ...

  2. JAVA中关于set()和get()方法的理解及使用

    对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 为了让JAVA初学者能更好的理解这两个方法的使用和意义,今天笔者 ...

  3. FBV和CBV区别

    FBV和CBV区别         def dispatch(self, request, *args, **kwargs):        # 做分发的        if request.meth ...

  4. 《GPU高性能编程CUDA实战》第九章 原子性

    ▶ 本章介绍了原子操作,给出了基于原子操作的直方图计算的例子. ● 章节代码 #include <stdio.h> #include "cuda_runtime.h" ...

  5. SparkSession

    在2.0版本之前,使用Spark必须先创建SparkConf和SparkContext catalog:目录 Spark2.0中引入了SparkSession的概念,SparkConf.SparkCo ...

  6. leetcode754

    class Solution { public: int reachNumber(int target) { // 理解这题的意思 这题就好做了 // 分析 首先考虑一种比较极端的情况 即一直向正方向 ...

  7. CSS3d 基础

    -webkit-transform-style:-webkit-preserve-3d;//设置3D转换 translateX:px; 平移 translateY:px; translateZ:px; ...

  8. Java架构技术知识点梳理

    Java程序员应该加以重视: 吃透基础技术 养成良好的阅读源码的习惯 有长期的技术学习规划 下面,我们来一起逐条看看,特别是第 3 个方法. 想要做到年薪50万,首先你自己必须是高水平的程序员! 能年 ...

  9. AS3 - 数组元素乱序方法以及效率比较

    http://www.hangge.com/blog/cache/detail_453.html

  10. hibernate 三种状态

    JPA是Java Persistence API的简称,中文名Java持久层API. 因JPA是由Hibernate的创建者编写,因此和Hibernate基本相似一致.JPA由不同的服务解析,因此在w ...