题意:DAG上有边权,求到终点dis期望
从1开始搜每一条路,分别记录dis与概率
搜到n让答案+=dis*概率即可
#include<cstdio> #include<iostream> #include<cstring> #include<cctype> #include<algorithm> #include<queue> using namespace std; #define olinr return #define _ 0 #define love_nmr 0 #define DB double int n; int m; struct node { int to; int dis; int nxt; }edge[205050]; int head[105050]; DB ans; bool vis[105050]; int du[105050]; int cnt; inline int read() { int x=0,f=1; char ch=getchar(); while(!isdigit(ch)) { if(ch=='-') f=-f; ch=getchar(); } while(isdigit(ch)) { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*f; } inline void put(int x) { if(x<0) { x=-x; putchar('-'); } if(x>9) put(x/10); putchar(x%10+'0'); } inline void add(int from,int to,int dis) { cnt++; edge[cnt].dis=dis; edge[cnt].to=to; edge[cnt].nxt=head[from]; head[from]=cnt; } inline void dfs(int now,DB gl,int dis) { if(now==n) { ans+=(DB)gl*dis; return; } for(int i=head[now];i;i=edge[i].nxt) { int go=edge[i].to; if(!vis[go]) { vis[go]=true; dfs(go,gl*((DB)1/(DB)du[now]),dis+edge[i].dis); vis[go]=false; } } } int main() { n=read(); m=read(); for(int a,b,c,i=1;i<=m;i++) { a=read(); b=read(); c=read(); add(a,b,c); du[a]++; } vis[1]=true; dfs(1,1.0,0); printf("%.2lf",ans); olinr ~~(0^_^0)+love_nmr; }