AcWing 217. 绿豆蛙的归宿
原题链接
简单
作者:
最后五分钟
,
2024-04-09 23:20:37
,
所有人可见
,
阅读 4
#include<bits/stdc++.h>
#define LL long long
#define x first
#define y second
#define de(x) cout<<#x<<" = "<<x<<" "
#define deg(x) cout<<#x<<" = "<<x<<endl
using namespace std;
const int N=1e5+10,M=2*N;
typedef pair<int,int> PII;
int dout[N];
int h[N],e[M],ne[M],idx;
double w[M];
double f[N];
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
void dfs(int u)
{
if(f[u])return;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
dfs(j);
f[u]+=(f[j]+w[i])/dout[u];
}
}
int main()
{
int n,m;
cin>>n>>m;
memset(h,-1,sizeof h);
for(int i=1;i<=m;i++)
{
int a,b,w;
cin>>a>>b>>w;
add(a,b,w);
dout[a]++;
}
dfs(1);
printf("%.2f\n",f[1]);
return 0;
}