[BZOJ 3036] 绿豆蛙的归宿
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=3036
[算法]
拓扑排序 + 概率DP
[代码]
#include<bits/stdc++.h> using namespace std; #define MAXN 100010 struct edge { int to,w,nxt; } e[MAXN << 1]; int tot; queue< int > q; int head[MAXN],in[MAXN],out[MAXN]; double f[MAXN]; template <typename T> inline void read(T &x) { int f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } inline void addedge(int u,int v,int w) { tot++; e[tot] = (edge){v,w,head[u]}; head[u] = tot; } int main() { int n,m; read(n); read(m); for (int i = 1; i <= m; i++) { int a,b,c; read(a); read(b); read(c); addedge(b,a,c); out[a]++; in[a]++; } q.push(n); while (!q.empty()) { int cur = q.front(); q.pop(); for (int i = head[cur]; i; i = e[i].nxt) { int v = e[i].to , w = e[i].w; f[v] += (f[cur] + w) / (in[v] * 1.0); if (!(--out[v])) q.push(v); } } printf("%.2lf\n",f[1]); return 0; }