题意:给一个无向连通图。从点1出发,等概率地从当前点走向邻接的点,走到点n停下来。问路径上经过的边的权值的异或和的期望是多少。重复经过的边要重复异或。
2 <= N <= 100, M <= 10000
单独处理每一个bit。
这题必须从n到i算,因为从点1绕一圈回到点1的路径异或和不一定为0。
设f[i]为从i到n,路径异或和的当前bit为1的概率。则有转移方程
注意有自环,加无向边的时候加一次就好了。
#include <cstdio>
#include <vector>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <functional>
using namespace std;
#define MAXN 111
#define MAXM 10011
typedef long long LL;
struct EDGE {
int u, v, w;
};
const double eps = 1e-8;
typedef pair<int, int> pii;
struct GRAPH {
<vector<pii> > s;
vector
void ClearEdges() {
for (auto& i : s) {
.resize(0);
i}
}
void Init(int n) {
();
ClearEdges.resize(n + 1);
s}
void AddUndi(int u, int v, int w) {
[u].emplace_back(v, w);
sif (u != v) s[v].emplace_back(u, w);
}
};
struct MATRIX {
<vector<double> > s;
vector
() {}
MATRIX(size_t a, size_t b) {
MATRIX(a, b);
resize}
inline size_t row() const {
return s.size();
}
inline size_t col() const {
return s.at(0).size();
}
void resize(size_t a, size_t b) {
.resize(a);
sfor (size_t i = 0; i < a; ++i)
[i].resize(b);
s}
void clear() {
for (auto& i : s)
for (auto& j : i)
= 0;
j }
void swap_row(size_t i, size_t j, size_t k = 0) {
for (; k < col(); ++k)
(s[i][k], s[j][k]);
swap}
//s[i] -= s[j] * d
void sub_row(size_t i, size_t j, double d, size_t k = 0) {
for (; k < col(); ++k)
[i][k] -= d * s[j][k];
s}
//O(n^3)
void ToUpper(MATRIX& b) {
for (size_t i = 0; i < row(); ++i) {
double maxv = fabs(s[i][i]);
size_t mr = i;
for (size_t j = i + 1; j < row(); ++j) {
if (maxv < fabs(s[j][i])) {
= fabs(s[j][i]);
maxv = j;
mr }
}
(i, mr, i);
swap_row.swap_row(i, mr);
bif (maxv < eps) continue;
for (size_t j = i + 1; j < row(); ++j) {
double d = s[j][i] / s[i][i];
(j, i, d, i);
sub_row.sub_row(j, i, d);
b}
}
}
};
//ax = b
//b is ans
void solve_destory(MATRIX& a, MATRIX& b) {
.ToUpper(b);
afor (int i = a.row() - 1; i >= 0; --i) {
assert(fabs(a.s[i][i]) > eps);
.s[i][0] /= a.s[i][i];
bfor (int j = 0; j < i; ++j) {
.s[j][0] -= b.s[i][0] * a.s[j][i];
b}
}
}
int main() {
;
GRAPH gstatic int du[MAXN];
int n, m;
("%d%d", &n, &m);
scanf.Init(n);
gfor (int i = 0; i < m; ++i) {
int u, v, w;
("%d%d%d", &u, &v, &w);
scanf--u;
--v;
.AddUndi(u, v, w);
g++du[u];
if (u != v) {
++du[v];
}
}
double ans = 0;
(n, 1);
MATRIX b(n, n);
MATRIX afor (int k = 0; k < 31; ++k) {
.clear();
a.clear();
bint now = 1 << k;
.s[n-1][n-1] = 1;
afor (int i = 0; i < n-1; ++i) {
.s[i][i] = 1;
afor (auto e : g.s[i]) {
if (e.second & now) {
.s[i][0] += 1.0 / du[i];
b.s[i][e.first] += 1.0 / du[i];
a} else {
.s[i][e.first] -= 1.0 / du[i];
a}
}
}
(a, b);
solve_destory+= b.s[0][0] * now;
ans }
("%.3f", ans);
printf
return 0;
}