码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj1415 NOI2005聪聪和可可

时间:2017-02-06 13:31:35      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:int   using   bsp   set   c++   struct   getch   get   long   

%%%http://hzwer.com/2819.html

先各种暴力搞出来p[x][y](从x到y下一个最近应该到达的位子)

然后就记忆化搜索??(雾)

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 #define LD long double  
 4 #define  N 100005
 5 using namespace std;
 6 inline int ra()
 7 {
 8     int x=0,f=1; char ch=getchar();
 9     while (ch<0 || ch>9) {if (ch==-) f=-1; ch=getchar();}
10     while (ch>=0 && ch<=9) {x=x*10+ch-0; ch=getchar();}
11     return x*f;
12 }
13 const int M=1001;
14 int n,m,cnt,a,b;
15 int head[M],d[M],q[N];
16 int dis[M][M],p[M][M];
17 double f[M][M];
18 struct node{
19     int to,next;
20 }e[M<<1];
21 void insert(int x, int y)
22 {
23     e[++cnt].to=y;
24     e[cnt].next=head[x];
25     head[x]=cnt;
26     d[x]++;
27 }
28 double dp(int x, int y)
29 {
30     if (f[x][y]) return f[x][y];
31     if (x==y) return 0;
32     if (p[x][y]==y || p[p[x][y]][y]==y) return f[x][y]=1;
33     double tot=dp(p[p[x][y]][y],y);
34     for (int i=head[y];i;i=e[i].next)
35         tot+=dp(p[p[x][y]][y],e[i].to);
36     return f[x][y]=tot/(d[y]+1)+1;
37 }
38 void bfs(int x)
39 {
40     int l=0,r=1;
41     q[0]=x; dis[x][x]=0;
42     while (l<r)
43     {
44         int now=q[l++],tmp=p[x][now];
45         for (int i=head[now];i;i=e[i].next)
46             if (dis[x][e[i].to]==-1 || (1+dis[x][now]==dis[x][e[i].to] && tmp<p[x][e[i].to]))
47             {
48                 dis[x][e[i].to]=dis[x][now]+1;
49                 p[x][e[i].to]=tmp;
50                 if (!tmp) p[x][e[i].to]=e[i].to;
51                 q[r++]=e[i].to;
52             }
53     }
54 }
55 int main()
56 {
57     memset(dis,-1,sizeof(dis));
58     n=ra(); m=ra(); a=ra(); b=ra();
59     for (int i=1; i<=m; i++)
60     {
61         int x=ra(),y=ra();
62         insert(x,y); insert(y,x);
63     }
64     for (int i=1; i<=n; i++) bfs(i);
65     printf("%.3lf",dp(a,b));
66     return 0;
67 }

 

bzoj1415 NOI2005聪聪和可可

标签:int   using   bsp   set   c++   struct   getch   get   long   

原文地址:http://www.cnblogs.com/ccd2333/p/6369680.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!