(color{#FF003F}{ exttt {CF605E Intergalaxy Trips}})
先说做法。
(E_x) 表示到目前为止的答案,显然 (E_x) 不会从更大的地方转移。(E_x = sum_{i}E_iP_{x,i}prod_{j<i}(1-P_{x,j}))
直接做的话,问题在于你不知道 (E) 的大小关系。考虑动态维护,每次找个答案最小的点,显然它不会被其他点更新,用它去更新其他点,复杂度是 (O(n^2))。
每次 (E_x) 并不是真实的答案,可能与它的每条边都不存在,要除 (1-prod_i (1-P_{x,i}))
唯一需要证明的地方是 (E_x = sum_{i}E_iP_{x,i}prod_{j<i}(1-P_{x,j})),可能还有一种策略是并不全选比 (E_x) 小的,只选较小的几个。
证明:
假设 (x) 已经从若干个点处转移,现在考虑加入 (i) 点,满足 (frac{E_i}{1-prod_{j}(1-P_{i,j})}leq frac{E_x}{1-prod_{j}(1-P_{x,j})})
设 (P=P_{x,i},M=prod_{j}(1-P_{x,j}),C=frac{E_i}{1-prod_{j}(1-P_{i,j})},0leq P,Mleq 1)
满足的条件是 (Cleq frac{E_x}{1-M})
原答案是 (frac{E_x}{1-M}),加入后答案是 (frac{E_x+C*P*M}{1-M+M*P})
需要证明 (frac{E_x+C*P*M}{1-M+M*P}leq frac{E_x}{1-M})
(ecause Cleq frac{E_x}{1-M} leqfrac{E_x}{1+M})
( herefore C+C*Mleq E_x)
( herefore C*M*P+C*M^2*Pleq E_x*M*P)
两边同时加 (E_x-E_x*M) ,(E_x-E_x*M+C*M*P+C*M^2*Pleq E_x*M*P+E_x-E_x*M)
整理得:((E_x+C*P*M)*(1-M)leq E_x(1-M+M*P))
( herefore frac{E_x+C*P*M}{1-M+M*P}leq frac{E_x}{1-M})
所以加入 (i) 更优。
而显然从期望答案更大的点转移是不优的,所以最优策略是从答案比 (x) 小的所有点转移。
// Author -- Frame
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<iostream>
#define lowbit(x) ((x)&(-x))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s
",__LINE__,__FUNCTION__)
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x7fffffffffffffff;
const double eps=1e-10;
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>Finline _Tp max(const _Tp &a,const _Tp &b){return a<b?b:a;}
template <typename _Tp>Finline _Tp min(const _Tp &a,const _Tp &b){return a<b?a:b;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>Finline bool _cmp(const _Tp &a,const _Tp &b){return abs(a-b)<=eps;}
template <typename _Tp>Finline void read(_Tp &x)
{
register char ch(getchar());
bool f(false);
while(ch<48||ch>57) f|=ch==45,ch=getchar();
x=ch&15,ch=getchar();
while(ch>=48&&ch<=57) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
if(f) x=-x;
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args)
{
read(t);read(args...);
}
Finline int read_str(char *s)
{
register char ch(getchar());
while(ch==' '||ch=='
'||ch=='
') ch=getchar();
register char *tar=s;
*tar=ch,ch=getchar();
while(ch!=' '&&ch!='
'&&ch!='
'&&ch!=EOF) *(++tar)=ch,ch=getchar();
return tar-s+1;
}
const int N=1005;
double a[N][N];
double p[N],E[N];
bool vis[N];
int main()
{
int n;
read(n);
for(int i=1;i<=n;++i)
{
p[i]=1.0;
E[i]=1.0;
for(int j=1;j<=n;++j)
{
scanf("%lf",&a[i][j]);
a[i][j]/=100.0;
}
}
p[n]=0;
E[n]=0.0;
while(true)
{
double minn=1e18;
int pos=0;
for(int i=1;i<=n;++i)
{
if(vis[i]||p[i]==1.0) continue;
double tmp=E[i]/(1.0-p[i]);
if(tmp<minn)
{
minn=tmp;
pos=i;
}
}
if(!pos) break;
vis[pos]=true;
E[pos]=minn;
for(int i=1;i<=n;++i)
{
if(!vis[i])
{
E[i]+=minn*p[i]*a[i][pos];
p[i]*=(1.0-a[i][pos]);
}
}
}
printf("%.10lf
",E[1]);
return 0;
}