zoj 3640 Help Me Escape
Posted 日拱一卒 功不唐捐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zoj 3640 Help Me Escape相关的知识,希望对你有一定的参考价值。
Background
If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at the door. And unto thee shall be his desire, and thou shalt rule over him.
And Cain talked with Abel his brother: and
it came to pass, when they were in the field, that Cain rose up against Abel his
brother, and slew him.
And the LORD said unto Cain, Where is Abel thy
brother? And he said, I know not: Am I my brother‘s keeper?
And he said,
What hast thou done? the voice of thy brother‘s blood crieth unto me from the
ground.
And now art thou cursed from the earth, which hath opened her
mouth to receive thy brother‘s blood from thy hand;
When thou tillest
the ground, it shall not henceforth yield unto thee her strength; a fugitive and
a vagabond shalt thou be in the earth.
—— Bible Chapter 4
Now Cain is unexpectedly trapped in a cave with N paths. Due to LORD‘s punishment, all the paths are zigzag and dangerous. The difficulty of the ith path is ci.
Then we define f as the fighting capacity of Cain. Every day, Cain will be sent to one of the N paths randomly.
Suppose Cain is in front of the ith path. He can successfully take ti days to escape from the cave as long as his fighting capacity f is larger than ci. Otherwise, he has to keep trying day after day. However, if Cain failed to escape, his fighting capacity would increase ci as the result of actual combat. (A kindly reminder: Cain will never died.)
As for ti, we can easily draw a conclusion that ti is closely related to ci. Let‘s use the following function to describe their relationship:
After D days, Cain finally escapes from the cave. Please output the expectation of D.
Input
The input consists of several cases. In each case, two positive integers N and f (n ≤ 100, f ≤ 10000) are given in the first line. The second line includes N positive integers ci (ci ≤ 10000, 1 ≤ i ≤ N)
Output
For each case, you should output the expectation(3 digits after the decimal point).
Sample Input
3 1 1 2 3
Sample Output
6.889
题意:
n条路,每条路有一个ci
开始有战斗力f
每天随机选择一条路出逃,若f>ci,花费ti天出逃
否则,花费1天,战斗力加ci
问期望出逃天数
记忆化搜索:
#include<cmath> #include<cstdio> #include<cstring> using namespace std; double dp[20001]; int c[101],t[101]; bool v[20001]; int n,m; double dfs(int f) { if(v[f]) return dp[f]; double ans=0; for(int i=1;i<=n;i++) if(f>c[i]) ans+=1.0/n*t[i]; else ans+=1.0/n*(1+dfs(f+c[i])); v[f]=true; return dp[f]=ans; } int main() { double tmp=sqrt(5.0); while(scanf("%d%d",&n,&m)!=EOF) { memset(dp,0,sizeof(dp)); memset(v,0,sizeof(v)); for(int i=1;i<=n;i++) scanf("%d",&c[i]); for(int i=1;i<=n;i++) t[i]=int((1+tmp)/2*c[i]*c[i]); dfs(m); printf("%.3lf\n",dp[m]); } }
DP:
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; double dp[20001]; int c[101],t[101]; int n,m,maxn; int main() { double tmp=(1+sqrt(5.0))/2; while(scanf("%d%d",&n,&m)!=EOF) { memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) scanf("%d",&c[i]),maxn=max(maxn,c[i]); for(int i=1;i<=n;i++) t[i]=int(tmp*c[i]*c[i]); for(int i=maxn+maxn;i>=m;i--) for(int j=1;j<=n;j++) if(i>c[j]) dp[i]+=1.0/n*t[j]; else dp[i]+=1.0/n*(1+dp[i+c[j]]); printf("%.3lf\n",dp[m]); } }
以上是关于zoj 3640 Help Me Escape的主要内容,如果未能解决你的问题,请参考以下文章