Luogu2973:[USACO10HOL]赶小猪

Posted Cyhlnj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Luogu2973:[USACO10HOL]赶小猪相关的知识,希望对你有一定的参考价值。

题面

Luogu

Sol

\(f[i]\)表示炸弹到\(i\)不爆炸的期望
高斯消元即可
另外,题目中的概率\(p/q\)实际上为\(1-p/q\)

还有,谁能告诉我不加\(EPS\),为什么会输出\(-0.00000\)

# 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 __(100005);

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[_];
double ans, f[_], a[_][_], p, q;

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);
    q = Input(); q /= Input(); p = 1.0 - q;
    for(RG int i = 1, u, v; i <= m; ++i)
        u = Input(), v = Input(), Add(u, v), Add(v, u);
    for(RG int u = 1; u <= n; u++){
        a[u][u] = -1.0;
        for(RG int e = fst[u]; e != -1; e = nxt[e]) a[to[e]][u] = p / dg[u];
    }
    a[1][n + 1] = -1.0; Gauss();
    for(RG int i = 1; i <= n; ++i){
        f[i] *= q;
        if(fabs(f[i]) < 1e-9) f[i] = 0;
    }
    for(RG int i = 1; i <= n; ++i) printf("%.9lf\n", f[i]);
    return 0;
}

以上是关于Luogu2973:[USACO10HOL]赶小猪的主要内容,如果未能解决你的问题,请参考以下文章

[Luogu P2973&BZOJ 1778][USACO10HOL]赶小猪DOtP(高斯消元+期望)

[USACO10HOL]赶小猪

[USACO10HOL]赶小猪题解

Luogu2973 [USACO10HOL]Driving Out the Piggies G

bzoj1778 驱赶猪猡/luogu2937 赶小猪

概率与期望DP习题总结