You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.1 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 100010, M = N << 1;
int n, m;
int in[N], g[N]; // 入度入度的备份数组原因in在topsort中会不断变小受破坏
double f[N];
// 链式前向星
int e[M], h[N], idx, w[M], ne[M];
void add(int a, int b, int c = 0) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
void topsort() {
queue<int> q;
q.push(n);
f[n] = 0; // n到n的距离期望是0
while (q.size()) {
int u = q.front();
q.pop();
for (int i = h[u]; ~i; i = ne[i]) { // 枚举每条入边(因为是反向图)
int v = e[i];
f[v] += (f[u] + w[i]) / g[v];
in[v]--;
if (in[v] == 0) q.push(v);
}
}
}
int main() {
memset(h, -1, sizeof h);
cin >> n >> m;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
add(b, a, c); // 反向图,计算从n到1
in[a]++; // 入度
g[a] = in[a]; // 入度数量
}
topsort();
printf("%.2lf\n", f[1]);
return 0;
}