您的位置:首页 > 其它

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

2016-03-10 19:02 253 查看
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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: