## 绿豆蛙的归宿

### 因为从$$1$$到$$i$$，所有概率和i不为1

#### CODE（正推）:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
const int MAXX=100010;
double dp[MAXX],g[MAXX];
int tot,n,m;
inline void add(int x,int y,int z){
ver[++tot]=y;
edge[tot]=z;
oud[x]++;
ind[y]++;
}
inline void topsort(){
queue<int>q;
for(int i=1;i<=n;++i)if(!ind[i])q.push(i);
dp[1]=0.000;
g[1]=1.000;
while(q.size()){
int x=q.front();
q.pop();
int y=ver[i];
dp[y]+=(dp[x]*g[x]+(double)edge[i]*g[x])/(double)oud[x];
g[y]+=g[x]/(double)oud[x];
if(--ind[y]==0)q.push(y);
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;++i){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
}
topsort();
printf("%.2lf",dp[n]);
return 0;
}

#### CODE2（逆推 )为了方便更新我们建了反图，但是出度以原图为准

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
const int MAXX=100010;
int tot,n,m;
double dp[MAXX];
inline void add(int x,int y,int z){
ver[++tot]=y;
edge[tot]=z;
}
inline void topsort(){
queue<int>q;
dp[n]=0;
for(int i=1;i<=n;++i)if(!ind[i])q.push(i);
while(q.size()){
int x=q.front();
q.pop();
int y=ver[i];
dp[y]+=(dp[x]+(double)edge[i])/(double)oud[y];
if(--ind[y]==0)q.push(y);
}
}
}
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);
ind[x]++;
oud[x]++;
}
topsort();
printf("%0.2lf",dp[1]);
return 0;
}