Bzoj3143: [Hnoi2013]游走

题面

Bzoj

Sol

\(Zsy\)又在切大火题了

考虑暴力,因为它是无穷的,我们可以设\(f[i][j]\)表示走了\(i\)条边,到达\(j\)的概率,然后跑\(5000\)步就有\(50\)
那么边经过次数的期望就可以算出来
对这些边的期望排序,一一编号,注意\(n\)不要转移

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

IL ll Input(){
    RG char c = getchar(); RG ll x = 0, z = 1;
    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[_], nt[__], cnt, to[__], id[__], dg[_];
double f[2][_], g[__], ans;

IL void Add(RG int u, RG int v, RG int ID){
    id[cnt] = ID; to[cnt] = v; nt[cnt] = fst[u]; fst[u] = cnt++; ++dg[v];
}

int main(RG int argc, RG char* argv[]){
    n = Input(); m = Input(); Fill(fst, -1);
    for(RG int i = 1, a, b; i <= m; ++i)
        a = Input(), b = Input(), Add(a, b, i), Add(b, a, i);
    f[0][1] = 1;
    for(RG int d = 1, lst = 0, nxt = 1; d <= 5000; ++d){
        for(RG int u = 1; u < n; ++u){
            if(f[lst][u] == 0) continue;
            for(RG int e = fst[u]; e != -1; e = nt[e]){
                f[nxt][to[e]] += f[lst][u] / dg[u];
                g[id[e]] += f[lst][u] / dg[u];
            }
            f[lst][u] = 0;
        }
        swap(lst, nxt);
    }
    sort(g + 1, g + m + 1);
    for(RG int i = 1; i <= m; ++i) ans += g[i] * (m - i + 1);
    printf("%.3lf\n", ans);
    return 0;
}

考虑优化
换一种思维,设每条边经过次数的期望为\(g[i]\),每个点的度数为\(dg[i]\),到每个点的概率为\(f[i]\)
设第\(i\)条边连接\(u,v\)
那么\(g[i] = \frac{f[u]}{dg[u]}+\frac{f[v]}{dg[v]}\)
\(i\)连接的点集为\(S\)
那么\(f[i] = \sum_{j\in S}\frac{f[j]}{dg[j]}\)
那么就可以解方程组求出所有的\(f[i]\),从而\(g\)也就求出来了
注意\(f[1]\)最开始为\(1\),注意\(n\)不要转移

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

IL ll Input(){
    RG char c = getchar(); RG ll x = 0, z = 1;
    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[__], cnt, to[__], dg[_], ff[__], tt[__];
double g[__], ans, f[_], a[_][_];

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

IL void Gauss(){
    for(RG int i = 1; i < n; ++i)
        for(RG int j = i + 1; j <= n; ++j){
            RG double div = a[j][i] / a[i][i];
            for(RG int k = 1; k <= n + 1; ++k) a[j][k] -= a[i][k] * div;
        }
    for(RG int i = n; i; --i){
        f[i] = a[i][n + 1] / a[i][i];
        for(RG int j = i - 1; j; --j) a[j][n + 1] -= f[i] * a[j][i]; 
    }
}

int main(RG int argc, RG char* argv[]){
    n = Input(); m = Input(); Fill(fst, -1);
    for(RG int i = 1; i <= m; ++i)
        ff[i] = Input(), tt[i] = Input(), Add(ff[i], tt[i]), Add(tt[i], ff[i]);
    for(RG int u = 1; u < n; ++u){
        a[u][u] = -1;
        for(RG int e = fst[u]; e != -1; e = nxt[e]) a[to[e]][u] = 1.0 / dg[u];
    }
    a[n][n] = a[1][n + 1] = -1; Gauss();
    for(RG int i = 1; i <= m; ++i){
        if(ff[i] != n) g[i] = f[ff[i]] / dg[ff[i]];
        if(tt[i] != n) g[i] += f[tt[i]] / dg[tt[i]];
    }
    sort(g + 1, g + m + 1);
    for(RG int i = 1; i <= m; ++i) ans += g[i] * (m - i + 1);
    printf("%.3lf\n", ans);
    return 0;
}

转载于:https://www.cnblogs.com/cjoieryl/p/8405410.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值