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 82 83 84 85 86 87
| const int maxn = 100 + 10; const int maxm = 10000 + 10; double A[maxn][maxn]; double K[maxn]; int N, M;
class Graph { public: int tot; int head[maxn], ver[maxm << 1], nxt[maxm << 1], w[maxm << 1]; void clear() { tot = 1; Set(head, 0); Set(ver, 0); Set(nxt, 0); Set(w, 0); }
void add(int x, int y, int z) { ver[++tot] = y; w[tot] = z;
nxt[tot] = head[x]; head[x] = tot; } };
Graph G;
void init() { G.clear(); Set(A, 0); Set(K, 0); }
void Gauss() { _rep(i, 1, N) { int r = i; _rep(j, i + 1, N) if(fabs(A[j][i]) > fabs(A[r][i])) r = j; if(r != i) _rep(k, 1, N + 1) swap(A[i][k], A[r][k]);
double t = A[i][i]; _rep(k, i + 1, N + 1) A[i][k] /= t;
_rep(j, 1, N) if(i != j) { double t = A[j][i]; _rep(k, 1, N + 1) A[j][k] -= t * A[i][k]; } } }
double build() { double ans = 0.0; _for(i, 0, 30) { Set(A, 0); _for(u, 1, N) { A[u][u] = 1.0; for(int e = G.head[u]; e; e = G.nxt[e]) { int v = G.ver[e];
if((1 << i) & G.w[e]) A[u][v] += 1.0 / K[u], A[u][N + 1] += 1.0 / K[u]; else A[u][v] -= 1.0 / K[u]; } } A[N][N] = 1.0; Gauss(); ans += A[1][N + 1] * (double)(1 << i); }
return ans; }
int main() { freopen("input.txt", "r", stdin); scanf("%d%d", &N, &M); _rep(i, 1, M) { int u, v, w; scanf("%d%d%d", &u, &v, &w); G.add(u, v, w); K[u] += 1.0, K[v] += 1.0; if(u == v) K[u] -= 1.0; else G.add(v, u, w); } // then build matrix printf("%.3lf\n", build()); }
|