BZOJ 3036: 绿豆蛙的归宿 [期望DP]

3036: 绿豆蛙的归宿

题意:有向无环图1到n期望路径长度


连高斯消元都不用了...

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef unsigned long long ll;
const int N=1e5+5;
inline int read() {
    char c=getchar(); int x=0, f=1;
    while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
    while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
    return x*f;
}

int n, m, de[N], u, v;
struct edge{int v, w, ne;}e[N<<1];
int cnt=1, h[N];
inline void ins(int u, int v, int w) {e[++cnt]=(edge){v, w, h[u]}; h[u]=cnt;}
double f[N]; int vis[N];
double dfs(int u) {
	if(vis[u]) return f[u];
	vis[u]=1;
	for(int i=h[u];i;i=e[i].ne) f[u] += (double)(dfs(e[i].v) + e[i].w)/de[u];
	return f[u];
}
int main(){
	freopen("in","r",stdin);
	n=read(); m=read();
	for(int i=1; i<=m; i++) u=read(), v=read(), ins(u, v, read()), de[u]++;
	printf("%.2lf", dfs(1));
}

Copyright:http://www.cnblogs.com/candy99/
posted @ 2017-04-02 23:39  Candy?  阅读(190)  评论(0编辑  收藏  举报