1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| #include<bits/stdc++.h> using namespace std; const int max_n=22,max_m=452,inf=1000000009; inline int read(){ int x=0;bool w=0;char c=getchar(); while(c<'0' || c>'9') w|=c=='-',c=getchar(); while(c>='0' && c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return w?-x:x; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10^48); } struct graph{ int ct,hd[max_n],to[max_m],nx[max_m]; graph(){ct=1;} inline void add(int u,int v){ nx[++ct]=hd[u],hd[u]=ct,to[ct]=v; } }e;
struct LinerPro{ int n,m,id[max_n*2]; double a[max_n][max_n]; inline void init(int x,int y){ n=x,m=y; memset(a,0,sizeof(a)); for(register int i=1;i<=n;++i) id[i]=i; for(register int i=1;i<=m;++i) id[i+n]=0; } inline void turn(int x,int y){ swap(id[y],id[x+n]); double k=a[x][y]; a[x][y]=1; for(register int i=0;i<=n;++i) a[x][i]/=k; for(register int i=0;i<=m;++i) if(i!=x){ double k=a[i][y]; a[i][y]=0; for(register int j=0;j<=n;++j) a[i][j]-=a[x][j]*k; } } inline double ask(){ while(1){ double k=0; int x=0,y=0; for(register int i=1;i<=m;++i) if(a[i][0]<k) k=a[i][0],x=i; if(!x) break; for(register int i=1;i<=n;++i) if(a[x][i]<0){ y=i; break; } turn(x,y); } while(1){ double k=inf; int x=0,y=0; for(register int i=1;i<=n;++i) if(a[0][i]>0){ y=i; break; } if(!y) break; for(register int i=1;i<=m;++i) if(a[i][y]>0 && a[i][0]/a[i][y]<k) k=a[i][0]/a[i][y],x=i; turn(x,y); } return -a[0][0]; } }lp;
int n,m,sr,sk,t,deg[max_n]; double f[max_n][max_n][max_n];
inline double dfs(int u,int x,int p){ if(f[u][x][p]!=0) return f[u][x][p]; if(p>t || u==x) return 0; for(register int i=e.hd[u];i;i=e.nx[i]) for(register int j=e.hd[x];j;j=e.nx[j]) dfs(e.to[i],e.to[j],p+1); lp.init(deg[x],deg[u]); for(register int i=e.hd[u],c1=1;i;i=e.nx[i],++c1){ for(register int j=e.hd[x],c2=1;j;j=e.nx[j],++c2) lp.a[c1][c2]=f[e.to[i]][e.to[j]][p+1]+1; lp.a[c1][0]=1; } lp.a[0][0]=0; for(register int i=1;i<=deg[x];++i) lp.a[0][i]=1; return f[u][x][p]=1/lp.ask(); }
signed main(){ n=read(),m=read(),sr=read(),sk=read(),t=read(); for(register int i=1;i<=m;++i){ int u=read(),v=read(); e.add(u,v),++deg[u]; } for(register int i=1;i<=n;++i) e.add(i,i),++deg[i]; printf("%.3lf",dfs(sr,sk,0)); return 0; }
|