【BZOJ 1415】 [Noi2005]聪聪和可可

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <cstring>
 6  using  namespace std;
 7 
 8  int p[ 1001][ 1001];
 9  double ans[ 1001][ 1001];
10  int n,m;
11  int tot,g[ 1001],nnext[ 1001* 2],num[ 1001* 2];
12  int team[ 1000000];
13  int d[ 1001];
14  int head,tail;
15  bool b[ 1001];
16  void Add( int x, int y)
17 {
18     tot++;
19     nnext[tot]=g[x];
20     g[x]=tot;
21     num[tot]=y;
22 }
23  void SPFA( int t)
24 {
25      while(head<tail)
26     {
27          int x=team[++head]; b[x]= false;
28          for( int i=g[x];i;i=nnext[i])
29         {
30              int tmp=num[i];
31              if(d[tmp]>d[x]+ 1||(d[tmp]==d[x]+ 1&&x<p[tmp][t]))
32             {
33                 d[tmp]=d[x]+ 1;
34                 p[tmp][t]=x;
35                  if(!b[tmp]) 
36                 {
37                     team[++tail]=tmp;
38                     b[tmp]= true;
39                 }
40             }
41         }
42     }
43 }
44 
45  double DFS( int x, int y)
46 {
47  //     cout<< x<<' '<<y<<endl;
48      
49      if(x==y)  return  0.0;
50      if(ans[x][y])  return ans[x][y];
51      if(p[x][y]==y) return  1.0; if(p[p[x][y]][y]==y)  return  1.0;
52     
53      double aa= 0;
54      int cnt= 0;
55      for( int i=g[y];i;i=nnext[i])
56         aa+=DFS(p[p[x][y]][y],num[i])+ 1.0,cnt++;
57     aa+=DFS(p[p[x][y]][y],y)+ 1.0;
58     aa/=( double)(cnt+ 1);
59     ans[x][y]=aa;
60      return ans[x][y];
61 }
62 
63  int main()
64 {
65     cin>>n>>m; int s,t;cin>>s>>t;
66      for( int i= 1;i<=m;i++)
67     {
68          int x,y;
69         cin>>x>>y;
70         Add(x,y);
71         Add(y,x);
72     }    
73         
74  //     cout<<"fasfa";
75      
76      for( int i= 1;i<=n;i++)
77     {
78         memset(d, 63, sizeof(d));
79         d[i]= 0;
80         head=tail= 0;
81         team[++tail]=i;b[i]= true;
82         SPFA(i);
83     }
84     
85      //     cout<<endl;
86      printf( " %.3lf\n ",DFS(s,t));
87      return  0;
88 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值