ZOJ-3640 Help Me Escape 概率DP
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3640
题意:Cain被困在一个洞穴里,洞穴有n个出口,每个出口有一个难度值C[i],Cain有一个初始的战斗值f。现在Cain随机选择一个出口,如果f大于出后的难度,那么Cain将会花floor( (1+sqrt(5))/2*C[i]*C[i] )天出去,否则Cain的f将会增加C[i]并且消耗掉一天时间,然后重新尝试。求Cain逃出洞穴天数的期望值。。。
首先用BFS把Cain所有的f值的可能情况求出来,然后就可以列出方程,这里因为期望都是由大的f推过来,所以f直接从大到小递推消元就可以了。。
1 //STATUS:C++_AC_730MS_1436KB 2 #include <functional> 3 #include <algorithm> 4 #include <iostream> 5 //#include <ext/rope> 6 #include <fstream> 7 #include <sstream> 8 #include <iomanip> 9 #include <numeric> 10 #include <cstring> 11 #include <cassert> 12 #include <cstdio> 13 #include <string> 14 #include <vector> 15 #include <bitset> 16 #include <queue> 17 #include <stack> 18 #include <cmath> 19 #include <ctime> 20 #include <list> 21 #include <set> 22 #include <map> 23 using namespace std; 24 #pragma comment(linker,"/STACK:102400000,102400000") 25 //using namespace __gnu_cxx; 26 //define 27 #define pii pair<int,int> 28 #define mem(a,b) memset(a,b,sizeof(a)) 29 #define lson l,mid,rt<<1 30 #define rson mid+1,r,rt<<1|1 31 #define PI acos(-1.0) 32 //typedef 33 //typedef __int64 LL; 34 //typedef unsigned __int64 ULL; 35 //const 36 const int N=40010; 37 const int INF=0x3f3f3f3f; 38 //const LL MOD=1000000007,STA=8000010; 39 //const LL LNF=1LL<<55; 40 const double EPS=1e-9; 41 const double OO=1e50; 42 const int dx[8]={-1,-1,0,1,1,1,0,-1}; 43 const int dy[8]={0,1,1,1,0,-1,-1,-1}; 44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 45 //Daily Use ... 46 inline int sign(double x){return (x>EPS)-(x<-EPS);} 47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;} 48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;} 49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;} 50 template<class T> inline T Min(T a,T b){return a<b?a:b;} 51 template<class T> inline T Max(T a,T b){return a>b?a:b;} 52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);} 53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);} 54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));} 55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));} 56 //End 57 58 double E[N],d[N]; 59 int w[N],id[N],c[N],vis[N]; 60 int n,f,cnt; 61 62 void bfs(int s) 63 { 64 int i,x; 65 queue<int> q; 66 q.push(s); 67 mem(vis,0); 68 vis[s]=1; 69 while(!q.empty()){ 70 x=q.front();q.pop(); 71 for(i=1;i<=n;i++){ 72 if(x>c[i] || vis[x+c[i]])continue; 73 w[cnt++]=x+c[i]; 74 vis[x+c[i]]=1; 75 q.push(x+c[i]); 76 } 77 } 78 } 79 80 int main(){ 81 // freopen("in.txt","r",stdin); 82 int i,j; 83 double t=(1+sqrt(5.0))/2,p; 84 while(~scanf("%d%d",&n,&f)) 85 { 86 p=1.0/n; 87 for(i=1;i<=n;i++){ 88 scanf("%d",&c[i]); 89 d[i]=floor(t*c[i]*c[i]); 90 } 91 92 w[cnt=0]=f;cnt++; 93 bfs(f); 94 sort(w,w+cnt); 95 for(i=0;i<cnt;i++)id[w[i]]=i; 96 for(i=cnt-1;i>=0;i--){ 97 E[i]=0; 98 for(j=1;j<=n;j++){ 99 if(w[i]>c[j])E[i]+=p*d[j]; 100 else E[i]+=p*(E[id[w[i]+c[j]]]+1); 101 } 102 } 103 104 printf("%.3lf\n",E[0]); 105 } 106 return 0; 107 }