bzoj2337高斯消元求期望
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 120
#define maxm 22000
#define eps 1e-7
int nn;
int e[maxn],ne[maxm],v[maxm],w[maxm];
void add(int x,int y,int z){
nn++;
ne[nn]=e[x];
e[x]=nn;
v[nn]=y;
w[nn]=z;
}
double f[maxn][maxn];
int n,m;
int d[maxn];
double a[maxn];
void solv(){
double k;
int p;
for(int i=1;i<=n;i++){
for(int j=p=i;j<=n;j++)if(fabs(f[j][i])>fabs(f[p][i]))p=j;
for(int j=0;j<=n;j++)swap(f[i][j],f[p][j]);
for(int j=i+1;j<=n;j++){
k=f[j][i]/f[i][i];
for(int z=0;z<=n;z++)f[j][z]-=f[i][z]*k;
}
}
for(int i=n;i;i--){
for(int j=1;j<=n;j++)f[i][0]-=a[j]*f[i][j];
a[i]=f[i][0]/f[i][i];
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
d[a]++;
if(a!=b){d[b]++;
add(b,a,c);}
}
double ans;
for(int t=0;t<31;t++){
memset(f,0,sizeof(f));
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)f[i][i]=1.0;
for(int i=1;i<n;i++){
for(int j=e[i];j;j=ne[j]){
if((1<<t)&w[j])f[i][v[j]]+=1.0/double(d[i]),f[i][0]+=1.0/double(d[i]);
else f[i][v[j]]-=1.0/double(d[i]);
}
}
solv();
ans+=a[1]*double(1<<t);
}
printf("%.3f",ans);
}