广搜记录每条路径的每个点的概率,重复路重复记录,按权重累计求和
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int nn=1e5+50;
inline int read()
{
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
vector<pair<int,int>>a[nn];
queue<int>q;
double cc[nn];
int main()
{
int n,m;
int u,v,w;
double ans=0;
n=read(),m=read();
for(int i=1;i<=m;++i)
{
u=read(),v=read();
w=read();
a[u].push_back(make_pair(v,w));
}
int s=1;
q.push(s);
int now=0,to;
cc[1]=1;
int len;
while(!q.empty())
{
now=q.front();
q.pop();
len=a[now].size();
for(int i=0;i<len;++i)
{
to=a[now][i].first;
cc[to]=cc[now]/(double)len;
ans+=cc[to]*a[now][i].second;
q.push(to);
}
}
ans*=100;
ans=round(ans);
ans/=100;
printf("%.2lf",ans);
return 0;
}