题面

题目传送门


分析

令爆炸概率为PPP。设 f(i)=∑k=0∞pk(i)\large f(i)=\sum_{k=0}^{\infty}p_k(i)f(i)=∑k=0∞​pk​(i),pk(i)p_k(i)pk​(i)表示经过kkk步走到iii的概率,那么在iii点结束的概率就为f(i)∗Pf(i)*Pf(i)∗P。

看看f(i)f(i)f(i)满足什么转移方程式。如下

f(i)=∑i−j(f(j)∗(1−P)/dj)\large f(i)=\sum_{i-j}(f(j)*(1-P)/d_j)f(i)=i−j∑​(f(j)∗(1−P)/dj​)

特别的,对于起点SSS

f(S)=∑S−j(f(j)∗(1−P)/dj)+1\large f(S)=\sum_{S-j}(f(j)*(1-P)/d_j)+1f(S)=S−j∑​(f(j)∗(1−P)/dj​)+1

那么我们将左边移到右边,再把f(S)f(S)f(S)的等式中+1+1+1移到左边,就得到一个nnn元方程组,高斯消元计算即可。

不知为什么原因WA?

这道题嘴上说着"误差不超过(1e-6)的答案会被接受",但其实没有SPJ,必须输出九位小数,那么问题出现了,由于精度问题,高斯消元本该得到的答案为000,但却得到了负零点几,那么直接输出就会输出"-0.000000000",于是WA也。

所以我们要在输出时判断一下是不是小于(1e-9)就行了。

不过网上大多题解都是将等式右面往左边移,系数就全部取反了,这样也能过。不过为了避免输出-0,输出小数都还是特判一下吧。

CODE(左往右+特判)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 305;
const double eps = 1e-15;
int n, m, A, B, d[MAXN];
double P, a[MAXN][MAXN];
inline void Guass(int N) {
for(int j = 1; j <= N; ++j) {
if(!a[j][j]) {
for(int i = j+1; i <= N; ++i)
if(a[i][j]) {
for(int k = j; k <= N+1; ++k)
swap(a[i][k], a[j][k]);
break;
}
}
for(int i = j+1; i <= N; ++i) {
double v = a[i][j] / a[j][j];
for(int k = j; k <= N+1; ++k)
a[i][k] -= v*a[j][k];
}
}
for(int i = N; i >= 1; --i) {
for(int j = i+1; j <= N; ++j)
a[i][N+1] -= a[j][N+1] * a[i][j];
a[i][N+1] /= a[i][i];
}
} int main () {
scanf("%d%d%d%d", &n, &m, &A, &B); P = (double)A/B;
for(int i = 1, x, y; i <= m; ++i)
scanf("%d%d", &x, &y), a[x][y] += 1, a[y][x] += 1, ++d[x], ++d[y];
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
if(d[j]) a[i][j] /= d[j];
a[i][j] *= (1-P);
}
a[i][i] -= 1;
}
a[1][n+1] = -1;
Guass(n);
for(int i = 1; i <= n; ++i)
printf("%.9f\n", fabs(a[i][n+1]*P) < (1e-9) ? 0 : a[i][n+1]*P);
}
CODE2(右移左+无特判)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 305;
const double eps = 1e-15;
int n, m, A, B, d[MAXN];
double P, a[MAXN][MAXN];
inline void Guass(int N) {
for(int j = 1; j <= N; ++j) {
if(!a[j][j]) {
for(int i = j+1; i <= N; ++i)
if(a[i][j]) {
for(int k = j; k <= N+1; ++k)
swap(a[i][k], a[j][k]);
break;
}
}
for(int i = j+1; i <= N; ++i) {
double v = a[i][j] / a[j][j];
for(int k = j; k <= N+1; ++k)
a[i][k] -= v*a[j][k];
}
}
for(int i = N; i >= 1; --i) {
for(int j = i+1; j <= N; ++j)
a[i][N+1] -= a[j][N+1] * a[i][j];
a[i][N+1] /= a[i][i];
}
} int main () {
scanf("%d%d%d%d", &n, &m, &A, &B); P = (double)A/B;
for(int i = 1, x, y; i <= m; ++i)
scanf("%d%d", &x, &y), a[x][y] += 1, a[y][x] += 1, ++d[x], ++d[y];
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
if(d[j]) a[i][j] /= d[j];
a[i][j] *= (P-1);
}
a[i][i] += 1;
}
a[1][n+1] = 1;
Guass(n);
for(int i = 1; i <= n; ++i)
printf("%.9f\n", a[i][n+1]*P);
}
05-11 22:33