Bzoj1415: [Noi2005]聪聪和可可

Posted Cyhlnj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

以上是关于Bzoj1415: [Noi2005]聪聪和可可的主要内容,如果未能解决你的问题,请参考以下文章

bzoj 1415: [Noi2005]聪聪和可可

BZOJ 1415 [Noi2005]聪聪和可可

BZOJ1415NOI2005聪聪和可可(动态规划,数学期望)

bzoj 1415: [Noi2005]聪聪和可可

Bzoj1415: [Noi2005]聪聪和可可

BZOJ 1415 NOI2005 聪聪和可可