Description
Input
Output
Sample Input
Sample Output
HINT
Source
既然是异或 那么按位算贡献即可
然后就是dp[i]表示i到N的路径上这一位为1的概率 高消乱搞即可
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
const int maxm = 20020;
const double eps = 1e-9;
int n, m;
double p[maxn][maxn], ans[maxn], ret;
inline void gauss()
{
for( int i = 1 ; i <= n ; i++ )
{
int t = i;
for( int j = i + 1 ; j <= n ; j++ ) if( fabs( p[ j ][ i ] ) > fabs( p[ t ][ i ] + eps) ) t = j;
for( int j = i ; j <= n + 1 ; j++ ) swap( p[ i ][ j ], p[ t ][ j ] );
for( int j = i + 1 ; j <= n ; j++ )
{
double t = p[ j ][ i ] / p[ i ][ i ];
for( int k = i ; k <= n + 1 ; k++ ) p[ j ][ k ] -= p[ i ][ k ] * t;
}
}
for( int i = n ; i ; i-- )
{
double cur = p[ i ][ n + 1 ];
for( int j = i + 1 ; j <= n ; j++ ) cur -= p[ i ][ j ] * ans[ j ];
ans[ i ] = cur / p[ i ][ i ];
}
}
struct edge
{
int to, nxt, val;
}e[ maxm ];
int head[maxn], cnt, d[maxn];
inline void addedge(int x, int y, int w)
{
e[ ++cnt ].to = y; e[ cnt ].nxt = head[ x ]; head[ x ] = cnt; e[ cnt ].val = w; d[ y ]++;
}
int main()
{
scanf( "%d%d", &n, &m );
for( int i = 1 ; i <= m ; i++ )
{
int x, y, w;
scanf( "%d%d%d", &x, &y, &w );
addedge( x, y, w );
if( x ^ y ) addedge( y, x, w );
}
for( int i = 0 ; i <= 30; i++ )
{
memset( ans, 0, sizeof( ans ) );
memset( p, 0, sizeof( p ) );
for( int x = 1 ; x <= n - 1 ; x++ )
{
p[ x ][ x ] = -1;
for( int j = head[ x ] ; j ; j = e[ j ].nxt )
{
if( ( e[ j ].val >> i ) & 1 ) p[ x ][ n + 1 ] -= 1.0 / d[ x ], p[ x ][ e[ j ].to ] -= 1.0 / d[ x ];
else p[ x ][ e[ j ].to ] += 1.0 / d[ x ];
}
}
p[ n ][ n ] = 1;
gauss();
ret += ( 1 << i ) * ans[ 1 ];
}
return printf( "%.3lf\n", ret ), 0;
}