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));
}

转载于:https://www.cnblogs.com/candy99/p/6660283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值