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 73 74 75 76 77 78 79 80 81
|
#include <bits/stdc++.h> #define Go(i , x , y) for(register int i = x; i <= y; i++) #define God(i , y , x) for(register int i = y; i >= x; i--)
const int N = 100 + 5;
void chkmax(int& a , int b) {a = std::max(a, b);}
int n, m, mx , dg[N]; std::vector <std::pair <int,int> > G[N]; double g[N][N];
void Build(int bit) { memset(g , 0 , sizeof(g)); g[n][n] = 1; Go(i , 1 , n-1) { g[i][i] = -dg[i]; for(int j = 0; j < (int)(G[i].size()); j++) { int v = G[i][j].first , w = G[i][j].second; if(w&bit) { g[i][n+1]--, g[i][v]--; } else g[i][v]++; } } }
void Gauss() { Go(i , 1 , n) { int now = i; Go(j , i+1 , n) if(fabs(g[now][i]) < fabs(g[j][i])) now = j; Go(j , 1 , n+1) std::swap(g[now][j] , g[i][j]); Go(j , i+1 , n+1) g[i][j] /= g[i][i]; g[i][i] = 1; Go(j , i+1 , n) { Go(k , i+1 , n+1) { g[j][k] -= g[j][i] * g[i][k]; } g[j][i] = 0; } } God(i , n , 1) { Go(j , i+1 , n) g[i][n+1] -= g[i][j] * g[j][n+1]; g[i][n+1] /= g[i][i]; g[i][i] = 1; } } int main() { scanf("%d%d" , &n , &m); Go(i , 1 , m) { int u , v , w; scanf("%d%d%d" , &u , &v , &w); G[u].push_back({v, w}); ++dg[u]; if(u == v) continue; G[v].push_back({u , w}); ++dg[v]; chkmax(mx , w); }
double ans = 0; for(int bit = 1; bit <= mx; bit <<= 1) { Build(bit); Gauss(); ans += g[1][n+1] * bit; }
std::cout << std::fixed << std::setprecision(3) << ans << std::endl; return 0; }
|