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
", f[i]);
	return 0;
}

原文地址:https://www.cnblogs.com/cjoieryl/p/8405745.html