dfs不解释.
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
struct vv{
int to;
double w;
};
vector<vv>v[N];
double f[N];
double dfs(int x)
{
if(f[x]) return f[x];
int cnt=v[x].size();
for(int i=0;i<cnt;i++)
{
f[x]+=(v[x][i].w+dfs(v[x][i].to))/cnt;
}
return f[x];
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,wi;
cin>>x>>y>>wi;
v[x].push_back(vv{y,wi});
}//建图
printf("%.2lf\n",dfs(1));
return 0;
}
京公网安备 11010502036488号