​传送门​

F[x] 表示x 到终点的期望长度

P4316 绿豆蛙的归宿 [概率与期望]_c++

out 表示to 通向的点的个数 , 我们方向建边 , 边拓扑排序边更新答案

#include<bits/stdc++.h>
#define N 100050
#define M 200050
using namespace std;
int first[N],next[M],to[M],w[M],tot;
double F[N]; int n,m,in[N],out[N];
void add(int x,int y,int z){
next[++tot]=first[x],first[x]=tot,to[tot]=y,w[tot]=z;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int x,y,z; scanf("%d%d%d",&x,&y,&z);
add(y,x,z); in[x]++ , out[x]++;
}
queue<int> q; q.push(n); F[n] = 0;
while(!q.empty()){
int x = q.front(); q.pop();
for(int i=first[x];i;i=next[i]){
int t=to[i];
F[t] += (F[x] + w[i]) / out[t];
if(--in[t] == 0) q.push(t);
}
} printf("%0.2lf",F[1]); return 0;
}