http://acm.hdu.edu.cn/showproblem.php?pid=4405
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <algorithm> #define LL long long //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int N=100015; double ans[N]; int f[N]; int main() { //freopen("data.txt","r",stdin); int n,m; while(scanf("%d %d",&n,&m)!=EOF) { if(n==0&&m==0) break; memset(f,-1,sizeof(f)); while(m--) { int i,j; scanf("%d %d",&i,&j); f[i]=j; } for(int i=0;i<=n+5;++i) ans[i]=0.0; for(int i=n-1;i>=0;--i) { if(f[i]!=-1) { ans[i]=ans[f[i]]; }else { for(int j=i+1;j<=i+6;++j) { ans[i]+=(ans[j]+1.0)/6.0; } } } printf("%.4lf\n",ans[0]); } return 0; }