题目大意:给定 $DAG$ 带边权连通图,保证所有点都能到达终点 $n$,每个点等概率沿边走,求起点 $1$ 到终点 $n$ 的期望长度。
题解:拓扑,然后倒着$DP$就可以了
卡点:无
C++ Code:
#include <cstdio>
#define maxn 100010
using namespace std;
int n, m, oud[maxn], vis[maxn];
int q[maxn], h, t;
double f[maxn];
int head[maxn], cnt;
struct Edge {int to, nxt, w;
} e[maxn << 1];
void add(int a, int b, int c) {e[++cnt] = (Edge) {b, head[a], c}; head[a] = cnt;
}
int main() {scanf("%d%d", &n, &m);for (int i = 0; i < m; i++) {int a, b, c;scanf("%d%d%d", &a, &b, &c);add(b, a, c);oud[a]++;vis[a]++;}f[q[h = t = 0] = n] = 0;while (h <= t) {int u = q[h++];for (int i = head[u]; i; i = e[i].nxt) {int v = e[i].to;f[v] += (f[u] + e[i].w) / oud[v];--vis[v];if (!vis[v]) q[++t] = v;}}printf("%.2lf\n", f[1]);return 0;
}