【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 }
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 }
相关文章推荐
- iOS开发 提示框- UIAlertController(是UIAlertView和UIActionSheet的合二为一)
- cocos2d-x之物理引擎之碰撞监测
- CSS盒子模型
- 【洛谷 1338】 末日的传说
- PDO中文乱码的问题
- 【洛谷 1357】 花园
- 【BZOJ 1012】 [JSOI2008]最大数maxnumber
- 【洛谷 2161】 [Shoi2009]Booking 会场预约
- 【BZOJ 1798】[Ahoi2009]Seq 维护序列seq
- 【CodeVS 1540】银河英雄传说 2002年NOI全国竞赛
- 【洛谷 1991】 无线通讯网
- 【洛谷 1345】 奶牛的电信
- 【BZOJ 3990】 [SDOI2015]排序
- 【BZOJ 2705】 [SDOI2012]Longge的问题
- 【BZOJ 1934】 [Shoi2007]Vote 善意的投票
- 【BZOJ 1433】 [ZJOI2009]假期的宿舍
- 【COGS 1873】 [国家集训队2011]happiness(吴确)
- 【Poj 3469】 Dual Core CPU
- 【BZOJ 3504 】[Cqoi2014]危桥
- 【CodeVS 1227】 方格取数2