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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值