P2973 [USACO10HOL]Driving Out the Piggies G
简单的概率DP。$f _ i$ 为 $i$ 点爆炸的概率,按照题意列方程即可。
查看代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include <cstdio> #include <cmath> #include <vector> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write(Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar(x % 10 + '0'); } const int N = 310; const int eps = 1e-12; int n, m, p, q; double A[N][N]; vector <int> g[N]; void Gauss() { for (int k = 0; k < n; k++) { int t = k; for (int i = k + 1; i < n; i++) fabs(A[i][k]) > fabs(A[t][k]) && (t = i); if (fabs(A[t][k]) < eps) continue; for (int i = k; i <= n; i++) swap(A[t][i], A[k][i]); for (int i = n; i >= k; i--) A[k][i] /= A[k][k]; for (int i = k + 1; i < n; i++) for (int j = n; fabs(A[i][k]) > eps && j >= k; j--) A[i][j] -= A[k][j] * A[i][k]; } for (int i = n - 1; i >= 0; i--) for (int j = i + 1; j < n; ++j) A[i][n] -= A[i][j] * A[j][n]; } int main () { read(n), read(m), read(p), read(q); double t = (double)p / q; for (int a, b; m; m--) { read(a), read(b); a--, b--; g[a].push_back(b); g[b].push_back(a); } for (int i = 0; i < n; i++) { A[i][i] = 1; for (int j : g[i]) A[i][j] = -(1 - t) / g[j].size(); } A[0][n] = t; Gauss(); for (int i = 0; i < n; i++) printf("%.9lf\n", A[i][n]); return 0; }
|