Coding Contest(费用流变形题,double)

时间:2024-11-24 16:37:01

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