zoukankan      html  css  js  c++  java
  • 洛谷P2973 [USACO10HOL]赶小猪(高斯消元 期望)

    题意

    题目链接

    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 << '
    ';}
    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
    ", a[i][N + 1] * (P / Q));
        return 0;
    }
    /*
    
    5 4
    1 2 2 1 3
    1 3
    
    */
    
  • 相关阅读:
    递归函数及Java范例
    笔记本的硬盘坏了
    “References to generic type List should be parameterized”
    配置管理软件(configuration management software)介绍
    WinCE文件目录定制及内存调整
    使用Silverlight for Embedded开发绚丽的界面(3)
    wince国际化语言支持
    Eclipse IDE for Java EE Developers 与Eclipse Classic 区别
    WinCE Heartbeat Message的实现
    使用Silverlight for Embedded开发绚丽的界面(2)
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10225843.html
Copyright © 2011-2022 走看看