BZOJ 3036: 绿豆蛙的归宿( 期望dp )

从终点往起点倒推 .

----------------------------------------------------------------------------

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>

#define clr( x , c ) memset( x , c , sizeof( x ) )
#define rep( i , n ) for( int i = 0 ; i < n ; ++i )
#define mp make_pair

using namespace std;

typedef pair< int , int > pii;

const int maxn = 1e5 + 5;

vector< pii > g[ maxn ]; // { to , dist }
int n;
double d[ maxn ];
bool vis[ maxn ];

void dfs( int x ) {
vis[ x ] = true;
int s = g[ x ].size();
rep( i , s ) {
if( ! vis[ g[ x ][ i ].first ] ) dfs( g[ x ][ i ].first );
d[ x ] += d[ g[ x ][ i ].first ] + g[ x ][ i ].second;
}
if( s ) d[ x ] /= s;
}

int main() {
// freopen( "test.in" , "r" , stdin );
int m;
cin >> n >> m;
while( m-- ) {
int u , v , d;
scanf( "%d%d%d" , &u , &v , &d );
--u , --v;
g[ u ].push_back( mp( v , d ) );
}
clr( d , 0 ) , clr( vis , 0 );
dfs( 0 );
printf( "%.2lf\n" , d[ 0 ] );
return 0;

----------------------------------------------------------------------------

3036: 绿豆蛙的归宿

Time Limit: 2 Sec  Memory Limit: 128 MB
Submit: 211  Solved: 149
[Submit][Status][Discuss]

4 4
1 2 1
1 3 2
2 3 3
3 4 4

7.00

Source

posted @ 2015-07-13 15:40  JSZX11556  阅读(276)  评论(0编辑  收藏  举报