51Nod 1450 闖關遊戲 —— 期望DP
阿新 • • 發佈:2018-09-14
namespace ble blank pro www. problem int pan cstring
題目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1450
期望DP;
INF 表示這種情況不行,轉移時把不行的概率也轉移到自身即可;
還要按得星概率排個序,先決策概率大的就是最優策略,因為後面的都基於它。
代碼如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int const maxn=2005,inf=1e9; int n,m;double f[maxn][maxn<<1]; struct N{double p1,p2,p0;}a[maxn]; bool cmp(N x,N y){return x.p2==y.p2?x.p1<y.p1:x.p2<y.p2;} int main() { scanf("%d%d",&n,&m); for(int i=1,x,y;i<=n;i++) { scanf("%d%d",&x,&y); a[i].p0=1.0*(1000-x-y)*0.001; a[i].p1=1.0*x*0.001; a[i].p2=1.0*y*0.001; } sort(a+1,a+n+1,cmp); for(int j=0;j<m;j++)f[n+1][j]=inf; for(int i=m,d=((n+1)<<1);i<=d;i++) f[n+1][i]=0; for(int i=n;i;i--) for(int j=0;j<=2*i;j++) { double k=1-a[i].p0; if(f[i+1][j+1]==inf&&f[i+1][j+2]==inf)f[i][j]=inf; else if(f[i+1][j+1]==inf)k=k-a[i].p1,f[i][j]=(f[i+1][j+2]*a[i].p2+1)/k; // else if(f[i+1][j+2]==inf)k=k-p[2][i],f[i][j]=(f[i+1][j+1]*p[1][i]+1)/k;//不會有 f[i+1][j+1]!=inf 而 f[i+1][j+2]=inf 的情況 else f[i][j]=(f[i+1][j+1]*a[i].p1+f[i+1][j+2]*a[i].p2+1)/k; } printf("%.8lf\n",f[1][0]); return 0; }
51Nod 1450 闖關遊戲 —— 期望DP