codeforces 678E Another Sith Tournament 概率dp
奉上官方题解
然后直接写的记忆化搜索
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <cstdio> #include <iostream> #include <ctime> #include <vector> #include <cmath> #include <map> #include <queue> #include <algorithm> #include <cstring> using namespace std; typedef long long LL; const int N=5e2+5; const int INF=0x3f3f3f3f; const int mod=1e9+7; double p[25][25],dp[1<<20][20]; int n; double gao(int s,int cur){ if(dp[s][cur]!=-1.0) return dp[s][cur]; for(int i=0;i<n;++i){ if(i!=cur&&(s&(1<<i))){ dp[s][cur]=max(dp[s][cur],p[cur][i]*gao(s^(1<<i),cur)+p[i][cur]*gao(s^(1<<cur),i)); } } return dp[s][cur]; } int main(){ scanf("%d",&n); for(int i=0;i<n;++i){ for(int j=0;j<n;++j) scanf("%lf",&p[i][j]); } for(int i=0;i<(1<<n);++i) for(int j=0;j<20;++j) dp[i][j]=-1; for(int i=1;i<n;++i) dp[(1<<i)][i]=0; dp[1][0]=1; double ans=0; for(int i=0;i<n;++i){ ans=max(ans,gao((1<<n)-1,i)); } printf("%.10f\n",ans); return 0; }