# CF605E Intergalaxy Trips

$E(u)=1+\left(\sum_i^{E(i)<E(u)}E(i)\times p_{u,i}\times\prod_{j}^{E(j)<E(i)}1-p_{u,j}\right)+E(u)\times\prod_i^{E(i)<E(u)}1-p_{u,i}$

$E(u)\times\left(1-\prod_i^{E(i)<E(u)}1-p_{u,i}\right)=1+\left(\sum_i^{E(i)<E(u)}E(i)\times p_{u,i}\times\prod_{j}^{E(j)<E(i)}1-p_{u,j}\right)$

$E(u)=\large\frac{1+\sum\limits_i^{E(i)<E(u)}E(i)\times p_{u,i}\times\prod\limits_{j}^{E(j)<E(i)}1-p_{u,j}}{1-\prod\limits_i^{E(i)<E(u)}1-p_{u,i}}$

code:

#include<bits/stdc++.h>
using namespace std;
#define N 1005
#define Db double
#define For(i,x,y)for(i=x;i<=(y);i++)
bool vis[N];
Db p[N][N],now[N],e[N];
{
int A;
bool K;
char C;
C=A=K=0;
while(C<'0'||C>'9')K|=C=='-',C=getchar();
while(C>'/'&&C<':')A=(A<<3)+(A<<1)+(C^48),C=getchar();
return(K?-A:A);
}
int main()
{
int n,i,j,o;
For(i,1,n)
vis[n]=1;
For(i,1,n)now[i]=1.0-p[i][n];
j=n;
For(o,1,n-1)
{
j=1;
For(i,2,n)
if(!vis[i]&&(e[i]+1.0)/(1.0-now[i])<(e[j]+1.0)/(1.0-now[j]))j=i;
vis[j]=1;
e[j]=(1.0+e[j])/(1.0-now[j]);
For(i,1,n)
if(!vis[i])e[i]+=e[j]*p[i][j]*now[i];
if(j==1)cout<<fixed<<setprecision(7)<<e[1],exit(0);
For(i,1,n)
if(!vis[i])now[i]*=1.0-p[i][j];
}
cout<<fixed<<setprecision(7)<<e[1];
return 0;
}

posted @ 2021-07-08 13:32  Biadocy  阅读(38)  评论(0编辑  收藏  举报