NC19030. Coding Contest
描述
输入描述
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.
输出描述
For each turn of each case, output the minimum possibility that the networks would break down. Round it to 2 digits.
示例1
输入:
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
输出:
0.50
C++14(g++5.4) 解法, 执行用时: 660ms, 内存消耗: 852K, 提交时间: 2018-10-07 15:23:11
#include<bits/stdc++.h> using namespace std;const int N=2e4+7;const double eps=1e-8,inf=-1e60; struct data{int to,next,v,from;double c;}e[N*10];int head[N],from[N],inq[N],cnt=1,S,T,T_T,t,w,x,n,m,i,j,q[N];double d[N],ans,p; void ins(int u,int v,int w,double c) {e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].v=w;e[cnt].c=c;e[cnt].from=u;} void insert(int u,int v,int w,double c){ins(u,v,w,c);ins(v,u,0,-c);} bool spfa(){ for(i=0;i<=T;++i)d[i]=inf;d[0]=S;inq[S]=1;q[0]=S;t=0;w=1; while(t!=w){ x=q[t++];if(t==N)t=0;inq[x]=0; for(i=head[x];i;i=e[i].next)if(e[i].v&&d[e[i].to]+eps<d[x]+e[i].c){ d[e[i].to]=d[x]+e[i].c; from[e[i].to]=i; if(!inq[e[i].to]){inq[e[i].to]=1;q[w++]=e[i].to;if(w==N)w=0;} } }return fabs(d[T]-inf)>eps; } void mincf(){ x=1e9;for(i=from[T];i;i=from[e[i].from])x=min(x,e[i].v); for(i=from[T];i;i=from[e[i].from])e[i].v-=x,e[i^1].v+=x,ans+=x*e[i].c; } int main(){ for(scanf("%d",&T_T);T_T--;memset(head,0,sizeof(head)),ans=0,cnt=1){ for(scanf("%d%d",&n,&m),S=0,T=n+1,x=1;x<=n;++x){ scanf("%d%d",&i,&j); if(i-j>0)insert(S,x,i-j,0); else if(i-j<0)insert(x,T,j-i,0); } for(;m--;){ scanf("%d%d%d%lf",&i,&j,&x,&p);p=1-p; if(x>0)insert(i,j,1,0); if(x>1)insert(i,j,x-1,log(p)); }while(spfa())mincf();printf("%.2f\n",1-exp(ans)); } }
C++11(clang++ 3.9) 解法, 执行用时: 691ms, 内存消耗: 960K, 提交时间: 2020-02-27 21:42:15
#include<bits/stdc++.h> using namespace std; const int N=2e4+7; const double eps=1e-8,inf=-1e60; struct data { int to,next,v,from; double c; }e[N*10]; int head[N],from[N],inq[N],cnt=1,S,T,T_T,t,w,x,n,m,i,j,q[N]; double d[N],ans,p; void ins(int u,int v,int w,double c) { e[++cnt].to=v; e[cnt].next=head[u]; head[u]=cnt; e[cnt].v=w; e[cnt].c=c; e[cnt].from=u; } void insert(int u,int v,int w,double c) { ins(u,v,w,c); ins(v,u,0,-c); } bool spfa() { for(i=0;i<=T;++i) d[i]=inf; d[0]=S; inq[S]=1; q[0]=S; t=0; w=1; while(t!=w) { x=q[t++]; if(t==N) t=0; inq[x]=0; for(i=head[x];i;i=e[i].next) if(e[i].v&&d[e[i].to]+eps<d[x]+e[i].c) { d[e[i].to]=d[x]+e[i].c; from[e[i].to]=i; if(!inq[e[i].to]) { inq[e[i].to]=1; q[w++]=e[i].to; if(w==N) w=0; } } } return fabs(d[T]-inf)>eps; } void mincf() { x=1e9; for(i=from[T];i;i=from[e[i].from]) x=min(x,e[i].v); for(i=from[T];i;i=from[e[i].from]) e[i].v-=x,e[i^1].v+=x,ans+=x*e[i].c; } int main() { for(scanf("%d",&T_T);T_T--;memset(head,0,sizeof(head)),ans=0,cnt=1) { for(scanf("%d%d",&n,&m),S=0,T=n+1,x=1;x<=n;++x) { scanf("%d%d",&i,&j); if(i-j>0) insert(S,x,i-j,0); else if(i-j<0) insert(x,T,j-i,0); } for(;m--;) { scanf("%d%d%d%lf",&i,&j,&x,&p); p=1-p; if(x>0) insert(i,j,1,0); if(x>1) insert(i,j,x-1,log(p)); } while(spfa()) mincf(); printf("%.2f\n",1-exp(ans)); } }