http://cogs.pro/cogs/problem/problem.php?pid=1065
题目大意就是,给一个DAG,求出期望路径长度。
那么我们计算一下每个点到终点步数的期望值,公式为f[i]=sigma{(f[j]+weight[j][i])/exit[j]},其中j为所有与i相连的点。
建反图倒推即可。f[1]即为所求。
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<queue> 6 using namespace std; 7 const int maxn=100005; 8 struct node 9 { 10 int from,to,next,weight; 11 }edge[maxn<<1]; 12 int head[maxn],chudu[maxn],n,m,tot,tmp[maxn]; 13 double f[maxn]; 14 void addedge(int u,int v,int w) 15 { 16 edge[++tot]=(node){u,v,head[u],w};head[u]=tot; 17 } 18 int haha() 19 { 20 freopen("ldfrog.in","r",stdin); 21 freopen("ldfrog.out","w",stdout); 22 scanf("%d%d",&n,&m); 23 for(int i=1;i<=m;i++) 24 { 25 int x,y,z;scanf("%d%d%d",&x,&y,&z); 26 addedge(y,x,z); 27 tmp[x]++;chudu[x]++; 28 } 29 queue<int>q;q.push(n); 30 while(!q.empty()) 31 { 32 int t=q.front();q.pop(); 33 for(int i=head[t];i;i=edge[i].next) 34 { 35 int v=edge[i].to; 36 tmp[v]--; 37 f[v]+=(f[t]+edge[i].weight)/chudu[v]; 38 if(!tmp[v])q.push(v); 39 } 40 } 41 printf("%0.2lf",f[1]); 42 } 43 int sb=haha(); 44 int main(){;}