Coding Contest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6071 Accepted Submission(s): 1430
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 si competitors 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
2016ACM/ICPC亚洲区青岛站-重现赛(感谢中国石油大学)
比较明显的费用流,只不过我们算出来的概率是乘积的形式,但是费用流一般是加法的形式,所以我们要对等式两边互取log,之后由于log是负数,所以我们加个负号变为正数,跑最小费用流即可。
注意:这道题有一个优化的点,如果我们拆点限制每个点会比较慢,这里我们可以通过差值,只添加一条边。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int N=410,M=2e4+10;
const double eps=1e-8;
int T,n,m,s,t,v[N],e[N];
int head[N],nex[M],to[M],flow[M],tot;
double w[M],d[M];
inline void ade(int a,int b,int c,double d){
to[++tot]=b; flow[tot]=c; w[tot]=d; nex[tot]=head[a]; head[a]=tot;
}
inline void add(int a,int b,int c,double d){
ade(a,b,c,d); ade(b,a,0,-d);
}
bool spfa(){
for(int i=1;i<=t;i++) d[i]=1e9; queue<int> q;
q.push(s); int vis[N]={0}; vis[s]=1;
while(q.size()){
int u=q.front(); q.pop(); vis[u]=0;
for(int i=head[u];i;i=nex[i]){
if(flow[i]&&d[to[i]]-d[u]-w[i]>eps){
d[to[i]]=d[u]+w[i];
v[to[i]]=u; e[to[i]]=i;
if(!vis[to[i]]) q.push(to[i]),vis[to[i]]=1;
}
}
}
return (d[t]-1e9<0);
}
double EK(){
double res=0;
while(spfa()){
int mi=0x3f3f3f3f;
for(int i=t;i!=s;i=v[i]) mi=min(mi,flow[e[i]]);
for(int i=t;i!=s;i=v[i]) flow[e[i]]-=mi,flow[e[i]^1]+=mi;
res+=mi*d[t];
}
return res;
}
int main(){
cin>>T;
while(T--){
tot=1; memset(head,0,sizeof head);
cin>>n>>m; t=n+1;
for(int i=1;i<=n;i++){
int a,b; scanf("%d %d",&a,&b);
if(a>b) add(s,i,a-b,0);
else add(i,t,b-a,0);
}
while(m--){
int a,b,c; double p; scanf("%d %d %d %lf",&a,&b,&c,&p);
if(c>=1) add(a,b,1,0);
if(c>1) add(a,b,c-1,-log2(1.0-p));
}
printf("%.2lf\n",1-pow(2,-EK()));
}
return 0;
}