Direct on-Memory search
#include <queue>#include<cstdio>#include<algorithm>using namespacestd;intRead_p,read_ca;inlineintRead () {read_p=0; read_ca=GetChar (); while(read_ca<'0'|| Read_ca>'9') read_ca=GetChar (); while(read_ca>='0'&&read_ca<='9') read_p=read_p*Ten+read_ca- -, read_ca=GetChar (); returnread_p;}structna{intY,ne;} b[10001];queue<int>Q;intA,BB;intn,m,l[3001],r[3001],ru[3001],num=0;intne[1001][1001],dis[1001][1001];Doublejy[1001][1001];BOOLbo[1001][1001];Const intinf=1e9;inlinevoidAddintXinty) {num++; if(!l[x]) l[x]=num;ElseB[r[x]].ne=num; B[num].y=y;r[x]=num;} InlinevoidBFsintx) { for(intI=1; i<=n;i++) dis[x][i]=INF; DIS[X][X]=0; ne[x][x]=x; for(intI=l[x];i;i=b[i].ne) dis[x][b[i].y]=1, ne[x][b[i].y]=B[i].y,q.push (B[I].Y); while(!Q.empty ()) { intk=Q.front (); Q.pop (); for(intI=l[k];i;i=b[i].ne)if(dis[x][b[i].y]>dis[x][k]+1) dis[x][b[i].y]=dis[x][k]+1, Ne[x][b[i].y]=ne[x][k],q.push (B[I].Y);Else if(dis[x][b[i].y]==dis[x][k]+1&&NE[X][B[I].Y]>NE[X][K]) ne[x][b[i].y]=Ne[x][k],q.push (B[I].Y); }}inlineDoubleDfsintAintBB) { if(A==BB)return 0.0; if(BO[A][BB])returnJY[A][BB]; BO[A][BB]=1; Doubleans=0.0; intAa=NE[NE[A][BB]][BB]; if(AA==BB)returnjy[a][bb]=1.0; intu=1; for(intI=l[bb];i;i=b[i].ne) Ans+=dfs (AA,B[I].Y), u++; Ans+=DFS (AA,BB); Ans=1.0/u*ans+1.0; returnjy[a][bb]=ans;}intMain () {intI,j,x,y; N=read (); m=read (); A=read (); bb=read (); for(i=1; i<=m;i++) X=read (), y=read (), add (x, y), add (y,x); for(intI=1; i<=n;i++) BFS (i); printf ("%.3lf\n", DFS (A,BB)); return 0;}
View Code
Bzoj 1415: [Noi2005] Cong and cocoa