Question: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4405
E [I]: expected number of steps to be taken at the current position I
Inspired by Liu rujia's suffix link Writing Method of the AC automatic machine, X [I] can reach the longest distance when "flight line" is continuous in reverse order,
rep(i,0,m) { scanf("%d%d",&xx,&yy); x[xx]=yy; } for(int i=n;i>=0;i--) if(x[i]!=-1 && x[x[i]] != -1) x[i]=x[x[i]];
I made a two-way mistake when I wrote it, and I thought I was wrong with the equation. For more information, see comments.
# Include <cstdio> # include <iostream> # include <cstring> using namespace STD; # define rep (I, S, e) for (INT I = s; I <E; I ++) # define repe (I, S, e) for (INT I = s; I <= E; I ++) # define reped (I, S, E) for (INT I = s; I> = E; I --) # define arclr (AA, v) memset (AA, V, sizeof (AA )) const Double P = 1.0/6; const int maxn = 100000 + 30; Double E [maxn], P2; int X [maxn]; int main () {// freopen ("hdu1_5.txt", "r", stdin); int XX, YY, n, m; while (~ Scanf ("% d", & N, & M) & N + M) {// P2 = m * 1.0/N; arclr (x, 0xff ); rep (I, 0, m) {scanf ("% d", & XX, & YY); X [XX] = YY ;}for (INT I = N; i> = 0; I --) if (X [I]! =-1 & X [x [I]! =-1) x [I] = x [x [I]; arclr (E, 0); reped (I, n-1, 0) {If (X [I] =-1) {reped (J, 6, 1) E [I] + = E [I + J]; E [I] = E [I]/6.0 + 1; continue;} If (X [I]! =-1 & X [x [I]! =-1) {e [x [I] = E [x [x [I]; X [I] = x [x [I]; // write it as X [I] = x [I + X [I]. The second is forced.} e [I] = E [x [I];} printf ("%. 4lf \ n ", E [0]);} return 0 ;}