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));
}
}

最新文章

  1. lvs+keepalived
  2. C#函数与SQL储存过程
  3. php WIN下编译注意问题
  4. atitit. orm mapping cfg 映射配置(3)-------hbnt one2maney cfg
  5. 一致性哈希算法学习及JAVA代码实现分析
  6. C#的对象内存模型
  7. C#基础(二)——C#中的构造函数
  8. iOS开发,新手入门指导
  9. jQuery.trim(str)
  10. 每天收获一点点------Hadoop Eclipse插件的使用
  11. Angular2入门-数据绑定
  12. ng-options语法详解
  13. js中的监听事件总结
  14. JAVA多线程-实现通讯
  15. 一张图看懂JVM
  16. angular学习一框架结构认识
  17. ubuntu 出现device not managed,解决方法
  18. 利用cocoapods创建基于git的私有库Spec Repo
  19. 【转】MFC 数据绑定 DoDataExchange( )
  20. 再不学会这些技巧,你就OUT了!

热门文章

  1. Parquet列式存储格式
  2. 在chrome中安装基于REST的web服务客户端
  3. JVM的DirectMemory设置
  4. 如何在一个js文件中引入另外的js文件
  5. python 简单的单例模式日志模块
  6. MySQL 创建数据库的两种方法
  7. 信息学奥赛(NOIP)复赛学习方法推荐
  8. python入门-类(一)
  9. Kafka集群扩展以及重新分布分区
  10. VisualSVN: 只能修改自己提交日志