Enumerate all vertices that cannot be passed through in sequence. The sum of probabilities of all other vertices is the probability that the vertex will not pass through.
#include <iostream>#include <cstdio>#include <queue>#include <algorithm>#include <cstring>#include <cmath>#include <queue>#include <iomanip>#include <vector>#define LL long long #define inf 1<<29using namespace std;struct node{int u,v;int next;}edge[250000];int n,m,d;int num;int head[55];int sum[55];double dp[11111][55];void addedge(int u,int v){edge[num].u=u;edge[num].v=v;edge[num].next=head[u];head[u]=num++;edge[num].u=v;edge[num].v=u;edge[num].next=head[v];head[v]=num++;}int main(){int t;int u,v;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&m,&d);num=0;memset(head,-1,sizeof(head));memset(dp,0,sizeof(dp));memset(sum,0,sizeof(sum));for(int i=0;i<m;i++){scanf("%d%d",&u,&v);addedge(u,v);sum[u]++;sum[v]++;}for(int i=1;i<=n;i++){memset(dp,0,sizeof(dp));for(int ii=1;ii<=n;ii++){dp[0][ii]=1.0/n;}for(int j=0;j<d;j++){for(int l=1;l<=n;l++){if(dp[j][l]!=0&&l!=i){int len=sum[l];double temp=dp[j][l]/len;//cout<<temp<<endl;for(int k=head[l];k!=-1;k=edge[k].next){int v=edge[k].v;dp[j+1][v]+=temp;}}}}double s=0;for(int j=1;j<=n;j++){if(j!=i){s+=dp[d][j];}}printf("%.9f\n",s);}}return 0;}
HDU-5001 walk 2014 Anshan Network Competition e question