Bzoj1415: [Noi2005]聪聪和可可

题面

Bzoj

Sol

就是求期望
预处理出可可在某一位置时聪聪下一步怎么走
然后按题意模拟,记搜

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2005);

IL ll Input(){
    RG ll x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m, fst[_], nxt[_], to[_], cnt, dis[_], pre[_][_], deg[_];
bool vis[_], G[_][_];
double f[_][_];
queue <int> Q;

IL void Add(RG int u, RG int v){
	to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++; ++deg[v];
}

IL void Bfs(RG int S){
	dis[S] = 0; Fill(vis, 0); vis[S] = 1; Q.push(S);
	while(!Q.empty()){
		RG int u = Q.front(); Q.pop();
		for(RG int e = fst[u]; e != -1; e = nxt[e]){
			if(vis[to[e]]) continue;
			dis[to[e]] = dis[u] + 1;
			vis[to[e]] = 1; Q.push(to[e]);
		}
	}
	for(RG int i = 1; i <= n; ++i)
		for(RG int e = fst[i]; e != -1; e = nxt[e]){
			if(dis[to[e]] + 1 != dis[i]) continue;
			if(!pre[S][i]) pre[S][i] = to[e];
			else pre[S][i] = min(pre[S][i], to[e]);
		}
}

IL double Dfs(RG int A, RG int B){
	if(G[A][B]) return f[A][B];
	G[A][B] = 1; RG int nt = pre[B][A];
	if(A == B) return f[A][B] = 0;
	if(nt == B || pre[B][nt] == B) return f[A][B] = 1;
	nt = pre[B][nt]; f[A][B] = Dfs(nt, B);
	for(RG int e = fst[B]; e != -1; e = nxt[e])	f[A][B] += Dfs(nt, to[e]);
	f[A][B] = f[A][B] / (deg[B] + 1) + 1;
	return f[A][B];
}

int main(RG int argc, RG char* argv[]){
	n = Input(); m = Input(); Fill(fst, -1);
	RG int A = Input(), B = Input();
	for(RG int i = 1; i <= m; ++i){
		RG int u = Input(), v = Input();
		Add(u, v); Add(v, u);
	}
	for(RG int i = 1; i <= n; ++i) Bfs(i);
	printf("%.3lf\n", Dfs(A, B));
	return 0;
}
posted @ 2018-02-06 20:32  Cyhlnj  阅读(120)  评论(0编辑  收藏  举报