Bzoj2337:[HNOI2011]XOR和路径

题面

bzoj

Sol

\(f[i]\)表示\(i到n\)的路径权值某一位为\(1\)的期望
枚举每一位,高斯消元即可
不要问我为什么是\(i\ - \ n\)而不可以是\(1\ - \ i\)

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(105);
const int __(20005);

IL ll Input(){
    RG ll x = 0, z = 1; RG char c = getchar();
    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[__], to[__], cnt, w[__], pw[_] = {1}, dg[_];
double a[_][_], f[_], ans;

IL void Add(RG int u, RG int v, RG int ww){
    w[cnt] = ww; 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] -= a[j][i] * f[i];
    }
}

int main(RG int argc, RG char* argv[]){
    n = Input(); m = Input(); Fill(fst, -1);
    for(RG int i = 1; i <= 30; ++i) pw[i] = pw[i - 1] << 1;
    for(RG int i = 1; i <= m; ++i){
        RG int u = Input(), v = Input(), ww = Input();
        Add(u, v, ww); if(u != v) Add(v, u, ww);
    }
    for(RG int i = 0; i <= 30; ++i){
        Fill(a, 0); a[n][n] -= 1.0;
        for(RG int u = 1; u < n; ++u){
            a[u][u] -= 1.0;
            for(RG int e = fst[u]; e != -1; e = nxt[e])
                if(~w[e] & pw[i]) a[u][to[e]] += 1.0 / dg[u];
                else a[u][n + 1] -= 1.0 / dg[u], a[u][to[e]] -= 1.0 / dg[u];
        }
        Gauss();
        ans += f[1] * pw[i];
    }
    printf("%.3lf\n", ans);
    return 0;
}
posted @ 2018-02-04 20:52  Cyhlnj  阅读(131)  评论(0编辑  收藏  举报