题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2337
题意:给定一个带权无向图。从1号点走到n号点。每次从当前点随机(等概率)选择一条相邻边走下去。每条到达n的路径的值为走过的边权的抑或。求期望。
思路:将权值按照二进制位一位一位进行。设f[i]表示从i节点走到n节点的期望。i的度数为d[i]。那么若一条边(i,j)的权值为0,则f[i]+=f[j]/d[i];否则f[i]+=(1-f[j])/d[i]。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <ctype.h>
#include <time.h>
#define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define CLR(x) x.clear()
#define ph(x) push(x)
#define pb(x) push_back(x)
#define Len(x) x.length()
#define SZ(x) x.size()
#define PI acos(-1.0)
#define sqr(x) ((x)*(x))
#define MP(x,y) make_pair(x,y)
#define EPS 1e-15
#define FOR0(i,x) for(i=0;i<x;i++)
#define FOR1(i,x) for(i=1;i<=x;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FORL0(i,a) for(i=a;i>=0;i--)
#define FORL1(i,a) for(i=a;i>=1;i--)
#define FORL(i,a,b)for(i=a;i>=b;i--)
#define rush() int CC;for(scanf("%d",&CC);CC--;)
#define Rush(n) while(scanf("%d",&n)!=-1)
using namespace std;
void RD(int &x){scanf("%d",&x);}
void RD(i64 &x){scanf("%lld",&x);}
void RD(u64 &x){scanf("%I64u",&x);}
void RD(u32 &x){scanf("%u",&x);}
void RD(double &x){scanf("%lf",&x);}
void RD(int &x,int &y){scanf("%d%d",&x,&y);}
void RD(i64 &x,i64 &y){scanf("%lld%lld",&x,&y);}
void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}
void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}
void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}
void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void RD(i64 &x,i64 &y,i64 &z){scanf("%lld%lld%lld",&x,&y,&z);}
void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}
void RD(char &x){x=getchar();}
void RD(char *s){scanf("%s",s);}
void RD(string &s){cin>>s;}
void PR(int x) {printf("%d\n",x);}
void PR(int x,int y) {printf("%d %d\n",x,y);}
void PR(i64 x) {printf("%lld\n",x);}
void PR(i64 x,i64 y) {printf("%lld %lld\n",x,y);}
void PR(u32 x) {printf("%u\n",x);}
void PR(u64 x) {printf("%llu\n",x);}
void PR(double x) {printf("%.3lf\n",x);}
void PR(double x,double y) {printf("%.5lf %.5lf\n",x,y);}
void PR(char x) {printf("%c\n",x);}
void PR(char *x) {printf("%s\n",x);}
void PR(string x) {cout<<x<<endl;}
void upMin(int &x,int y) {if(x>y) x=y;}
void upMin(i64 &x,i64 y) {if(x>y) x=y;}
void upMin(double &x,double y) {if(x>y) x=y;}
void upMax(int &x,int y) {if(x<y) x=y;}
void upMax(i64 &x,i64 y) {if(x<y) x=y;}
void upMax(double &x,double y) {if(x<y) x=y;}
const int mod=100000007;
const i64 inf=((i64)1)<<60;
const double dinf=1000000000000.0;
const int INF=1000000000;
const int N=105;
double g[N][N],ans[N];
int n,m,d[N];
vector<pair<int,int> > V[N];
int sgn(double x)
{
if(x>EPS) return 1;
if(x<-EPS) return -1;
return 0;
}
void Gauss()
{
int i,j,k;
double x;
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++) if(sgn(g[j][i])) break;
if(j>n) continue;
for(k=1;k<=n+1;k++) swap(g[i][k],g[j][k]);
for(k=i+1;k<=n;k++)
{
if(sgn(g[k][i])==0) continue;
x=g[k][i]/g[i][i];
for(j=i;j<=n+1;j++) g[k][j]-=g[i][j]*x;
}
}
for(i=n;i>=1;i--)
{
ans[i]=g[i][n+1];
for(j=i+1;j<=n;j++) ans[i]-=ans[j]*g[i][j];
ans[i]/=g[i][i];
}
}
int main()
{
RD(n,m);
int i,j,k,x,y,w;
FOR1(i,m)
{
RD(x,y,w);
d[x]++;
V[x].pb(MP(y,w));
if(x==y) continue;
d[y]++;
V[y].pb(MP(x,w));
}
double p=0,temp;
for(i=0;i<=30;i++)
{
clr(g,0);
FOR1(j,n)
{
temp=0; g[j][j]=1;
FOR0(k,SZ(V[j]))
{
y=V[j][k].first;
w=V[j][k].second;
if(w&(1<<i)) temp+=1.0/d[j],g[j][y]+=1.0/d[j];
else g[j][y]-=1.0/d[j];
}
g[j][n+1]=temp;
}
FOR1(j,n+1) g[n][j]=0;
g[n][n]=1;
Gauss();
p+=(1<<i)*ans[1];
}
PR(p);
}