DFS求期望就好了
#include <cstdio>
using namespace std;
inline int read(void)
{
int a=0,f=1;static char c;
while((c=getchar())<'0'||c>'9')c=='-'?f=-1:0;
while(c>='0'&&c<='9')
a=a*10+c-'0',c=getchar();
return a*f;
}
int n,m,out[100001];
double f[100001];
char vis[100001];
struct Edge
{
int to,cost;
Edge *next;
Edge(int y=0,int z=0):to(y),cost(z){}
}edge[200001];
Edge *link[100001];
void dfs(int x)
{
if(!vis[x]) vis[x]=1;
else return ;
for (Edge *i=link[x];i;i=i->next)
dfs(i->to),f[x]+=i->cost+f[i->to];
if(out[x]) f[x]/=out[x];
return ;
}
int main(void)
{
register int i,x,y,z;
n=read(),m=read();
for (i=1;i<=m;++i)
x=read(),y=read(),z=read(),edge[i]=Edge(y,z),edge[i].next=link[x],link[x]=&edge[i],++out[x];
dfs(1);
printf("%.2lf",f[1]);
return 0;
}