简单的概率DP,求期望,逆推更好。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define N 100005 using namespace std; double p[N]; struct e{ int u,v; int next; }edge[1010]; int head[N],tot; void addedge(int u,int v){ edge[tot].u=u; edge[tot].v=v; edge[tot].next=head[u]; head[u]=tot++; } int findx(int p){ int ret=p; for(int ei=head[p];ei!=-1;ei=edge[ei].next){ ret=edge[ei].v; } return ret; } int main(){ int n,m,u,v,pos; while(scanf("%d%d",&n,&m),n||m){ tot=0; memset(head,-1,sizeof(head)); for(int i=1;i<=m;i++){ scanf("%d%d",&u,&v); addedge(u,v); } p[n]=0; for(int i=n-1;i>=0;i--){ p[i]=0; if(head[i]!=-1){ p[i]=p[findx(i)]; } else{ for(int k=1;k<=6;k++){ pos=i+k>n?n:i+k; if(head[pos]!=-1){ p[i]+=p[findx(pos)]/6.0; } else{ p[i]+=p[pos]/6.0; } } p[i]+=1; } } printf("%.4lf ",p[0]); } return 0; }