欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

洛谷P2973 [USACO10HOL]赶小猪(高斯消元 期望)

程序员文章站 2022-11-10 22:19:25
题意 "题目链接" Sol 设$f[i]$表示炸弹到达$i$这个点的概率,转移的时候考虑从哪个点转移而来 $f[i] = \sum_{\frac{f(j) (1 \frac{p}{q})}{deg(j)}}$ $f[1]$需要+1(炸弹一开始在1) cpp // luogu judger enabl ......

题意

题目链接

sol

\(f[i]\)表示炸弹到达\(i\)这个点的概率,转移的时候考虑从哪个点转移而来

\(f[i] = \sum_{\frac{f(j) * (1 - \frac{p}{q})}{deg(j)}}\)

\(f[1]\)需要+1(炸弹一开始在1)

// luogu-judger-enable-o2
#include<bits/stdc++.h> 
#define pair pair<int, int>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long 
#define ll long long 
#define fin(x) {freopen(#x".in","r",stdin);}
#define fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int maxn = 3001, mod = 1e9 + 7, inf = 1e9 + 10;
const double eps = 1e-9;
template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;}
template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename a> inline void debug(a a){cout << a << '\n';}
template <typename a> inline ll sqr(a x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, m, vis[maxn][maxn], deg[maxn];
double p, q, a[maxn][maxn];
void gauss() {
    for(int i = 1; i <= n; i++) {
        int mx = i;
        for(int j = i + 1; j <= n; j++) if(a[j][i] > a[mx][i]) mx = j;
        if(mx != i) swap(a[i], a[mx]);
        for(int j = i + 1; j <= n + 1; j++) a[i][j] /= a[i][i]; a[i][i] = 1;
        for(int j = 1; j <= n; j++) {
            if(i == j) continue;
            double p = a[j][i] / a[i][i];
            for(int k = 1; k <= n + 1; k++) {
                a[j][k] -= a[i][k] * p;
            }
        }
    }
}
signed main() {
    n = read(); m = read(); p = read(); q = read();
    for(int i = 1; i <= m; i++) {
        int x = read(), y = read();
        vis[x][y] = vis[y][x] = 1;
        deg[x]++; deg[y]++;
    }
    for(int i = 1; i <= n; i++) {
        a[i][i] = 1;
        for(int j = 1; j <= n; j++) 
            if(vis[i][j]) 
                a[i][j] = -(1.0 - p / q) / deg[j];      
    }
    a[1][n + 1] = 1;
    gauss();
    for(int i = 1; i <= n; i++) printf("%.9lf\n", a[i][n + 1] * (p / q));
    return 0;
}
/*

5 4
1 2 2 1 3
1 3

*/