[tyvj1933]绿豆蛙的归宿

由于是无环图所以并不需要高斯消元什么的。。

f[i]:=i到n的期望

f[i]=Σf[j]+cost[i][j]    (i,j∈G)

f[n]=0

那么就是需要求f[1],将图反向后bfs一遍就行。(dfs的话会爆栈。。)

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <queue>
 7 #include <map>
 8 using namespace std;
 9 const int N=110000;
10 const int M=2*N;
11 int h[N],r[M],to[M],cost[M],tot;
12 void add(int u,int v,int c){
13     to[tot]=v;
14     cost[tot]=c;
15     r[tot]=h[u];
16     h[u]=tot++;
17 }
18 int n,m,a,b,c,k[N],d[N];
19 double f[N];
20 queue<int>q;
21 int main(){
22     memset(h,-1,sizeof(h));
23     scanf("%d%d",&n,&m);
24     for(int i=1;i<=m;i++){
25         scanf("%d%d%d",&a,&b,&c);
26         add(b,a,c);
27         k[a]++;d[a]++;
28     }
29     q.push(n);
30     while(q.size()){
31         int u=q.front();q.pop();
32         for(int i=h[u];i!=-1;i=r[i]){
33             int v=to[i];
34             f[v]+=(f[u]+cost[i])/(double)k[v];
35             d[v]--;
36             if(!d[v])q.push(v);
37         }
38     }
39     printf("%.2f\n",f[1]);
40 }
View Code

 

posted @ 2017-01-23 16:35  KingSann  阅读(141)  评论(0编辑  收藏  举报