topsort时顺便把路径长度期望算一下就行
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 1e5 + 7;
vector<PII> v[N];
//int h[N], w[N], e[N], ne[N], idx;
int n, m;
int doin[N], doout[N];
queue<int> q;
double baseC[N];
double ans = 0;
void topsort()
{
for (int i = 1; i <= n; i++){
if(doin[i] == 0){q.push(i);baseC[i] = 1;}
}
while (q.size())
{
int u = q.front();
q.pop();
double tmp = 1.0 / doout[u] * baseC[u];
for (int i = 0; i < v[u].size(); i++)
{
int x = v[u][i].first;
double y = v[u][i].second;
doin[x]--;
baseC[x] += tmp;
ans += tmp * y;
if (doin[x] == 0)q.push(x);
//cout << " * ";
}
}
printf("%.2lf\n", ans);
}
int main()
{
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int x, y, w;
cin >> x >> y >> w;
v[x].push_back({y, w});
doin[y]++, doout[x]++;
}
topsort();
return 0;
}