Files
python/TangDou/AcWing_TiGao/T5/QiWang/217.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

35 lines
836 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = N << 1;
int n, m;
int h[N], e[M], ne[M], w[M], idx;
int out[N];
double f[N];
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
double dfs(int u) {
if (f[u] >= 0) return f[u]; // 如果u点计算过,记忆化搜索返回结果值
f[u] = 0; // 初始化为0,准备开始填充
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
f[u] += (w[i] + dfs(j)) / out[u];
}
return f[u];
}
int main() {
memset(h, -1, sizeof h);
cin >> n >> m;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
out[a]++;
}
memset(f, -1, sizeof f);
printf("%.2lf\n", dfs(1));
return 0;
}