题解来源:http://www.cnblogs.com/jianglangcaijin/p/3799494.html
最后必然是走了一条链,或者是一个环(一直绕),或者是一条链加一个环。设f[i][j][k]表示从点j走了i步到达节点k的最大幸福度。那么f[i][j][j]就表示在绕环。那么在这个环上一直绕下去的期望为:
那么从S走i步到j再在j开始的环上绕圈的期望为:
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi acos(-1.0) # define eps 1e-3 # define MOD 1000000000 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } void Out(int a) { if(a<0) {putchar('-'); a=-a;} if(a>=10) Out(a/10); putchar(a%10+'0'); } const int N=105; //Code begin... double dp[N][N][N], val[N], Pow[N], sum[N]; int E[N*10][2]; int main () { int n, m, s, u, v; double p, ans=0; scanf("%d%d",&n,&m); FOR(i,1,n) scanf("%lf",val+i); scanf("%d%lf",&s,&p); FOR(i,1,m) scanf("%d%d",&E[i][0],&E[i][1]); FOR(i,0,n) FOR(j,1,n) FOR(k,1,n) dp[i][j][k]=-1e18; Pow[0]=1; FOR(i,1,n+1) Pow[i]=Pow[i-1]*p; FOR(i,1,n) sum[i]=dp[0][i][i]=val[i]; FOR(i,1,n) FOR(j,1,n) FOR(k,1,m) dp[i][j][E[k][1]]=max(dp[i][j][E[k][1]],dp[i-1][j][E[k][0]]+val[E[k][1]]*Pow[i]); FOR(j,1,n) FOR(i,1,n) sum[j]=max(sum[j],(dp[i][j][j]-val[j]*Pow[i])/(1-Pow[i])); FOR(i,1,n) FOR(j,1,n) if (dp[i][s][j]>=0) ans=max(ans,max(dp[i][s][j],dp[i][s][j]+sum[j]*Pow[i]-val[j]*Pow[i])); printf("%.1f ",ans); return 0; }