bzoj 1415: [Noi2005]聪聪和可可

直接上记忆化搜索

#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
int read_p,read_ca;
inline int read(){
read_p=;read_ca=getchar();
while(read_ca<''||read_ca>'') read_ca=getchar();
while(read_ca>=''&&read_ca<='') read_p=read_p*+read_ca-,read_ca=getchar();
return read_p;
}
struct na{
int y,ne;
}b[];
queue <int> q;
int a,bb;
int n,m,l[],r[],ru[],num=;
int ne[][],dis[][];
double jy[][];
bool bo[][];
const int INF=1e9;
inline void add(int x,int y){
num++;
if (!l[x]) l[x]=num;else b[r[x]].ne=num;
b[num].y=y;r[x]=num;
}
inline void bfs(int x){
for (int i=;i<=n;i++) dis[x][i]=INF;
dis[x][x]=;ne[x][x]=x;
for (int i=l[x];i;i=b[i].ne) dis[x][b[i].y]=,ne[x][b[i].y]=b[i].y,q.push(b[i].y);
while (!q.empty()){
int k=q.front();q.pop();
for (int i=l[k];i;i=b[i].ne)
if (dis[x][b[i].y]>dis[x][k]+) dis[x][b[i].y]=dis[x][k]+,ne[x][b[i].y]=ne[x][k],q.push(b[i].y);else
if (dis[x][b[i].y]==dis[x][k]+&&ne[x][b[i].y]>ne[x][k]) ne[x][b[i].y]=ne[x][k],q.push(b[i].y);
}
}
inline double dfs(int a,int bb){
if (a==bb) return 0.0;
if (bo[a][bb]) return jy[a][bb];
bo[a][bb]=;
double ans=0.0;
int aa=ne[ne[a][bb]][bb];
if (aa==bb) return jy[a][bb]=1.0;
int u=;
for (int i=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;
return jy[a][bb]=ans;
}
int main(){
int i,j,x,y;
n=read();m=read();
a=read();bb=read();
for (i=;i<=m;i++) x=read(),y=read(),add(x,y),add(y,x);
for (int i=;i<=n;i++) bfs(i);
printf("%.3lf\n",dfs(a,bb));
return ;
}
上一篇:快速排序 Python 模板


下一篇:JDK的帮助文档