【BZOJ】【1415】【NOI2005】聪聪和可可

数学期望+记忆化搜索


  论文:《浅析竞赛中一类数学期望问题的解决方法》——汤可因  中的第一题……

  Orz 黄学长

  我实在是太弱,这么简单都yy不出来……

  宽搜预处理有点spfa的感觉= =凡是更新了的,都要重新入队更新一遍……

  dp的记忆化搜索过程好厉害……

  期望这里一直很虚啊,赶紧再多做点题熟悉熟悉……

 1 /**************************************************************
 2     Problem: 1415
 3     User: Tunix
 4     Language: C++
 5     Result: Accepted
 6     Time:208 ms
 7     Memory:17240 kb
 8 ****************************************************************/
 9  
10 //BZOJ 1415
11 #include<cstdio>
12 #include<queue>
13 #include<cstring>
14 #include<cstdlib>
15 #include<iostream>
16 #include<algorithm>
17 #define rep(i,n) for(int i=0;i<n;++i)
18 #define F(i,j,n) for(int i=j;i<=n;++i)
19 #define D(i,j,n) for(int i=j;i>=n;--i)
20 #define pb push_back
21 using namespace std;
22 typedef long long LL;
23 inline int getint(){
24     int r=1,v=0; char ch=getchar();
25     for(;!isdigit(ch);ch=getchar()) if (ch=='-') r=-1;
26     for(; isdigit(ch);ch=getchar()) v=v*10-'0'+ch;
27     return r*v;
28 }
29 const int N=1010,M=1010;
30 /*******************template********************/
31  
32 struct edge{int to,next;}E[M<<1];
33 int head[N],cnt;
34 void add(int x,int y){
35     E[++cnt]=(edge){y,head[x]}; head[x]=cnt;
36     E[++cnt]=(edge){x,head[y]}; head[y]=cnt;
37 }
38  
39 int n,m,S,T,p[N][N],d[N][N],du[N];
40 queue<int>Q;
41 void bfs(int x){
42     Q.push(x);
43     d[x][x]=0;
44     while(!Q.empty()){
45         int now=Q.front(),tmp=p[x][now]; Q.pop();
46         for(int i=head[now];i;i=E[i].next)
47             if (d[x][E[i].to]==-1 ||
48                     (d[x][now]+1==d[x][E[i].to] && tmp<p[x][E[i].to])){
49                 d[x][E[i].to]=d[x][now]+1;
50                 p[x][E[i].to]=tmp;
51                 if (!tmp) p[x][E[i].to]=E[i].to;
52                 Q.push(E[i].to);
53             }
54     }
55 }
56 double f[N][N];
57 double dp(int x,int y){
58     if (f[x][y]) return f[x][y];
59     if (x==y) return 0;
60     if (p[x][y]==y || p[p[x][y]][y]==y) return f[x][y]=1;
61     double tot=dp(p[p[x][y]][y],y);
62     for(int i=head[y];i;i=E[i].next)
63         tot+=dp(p[p[x][y]][y],E[i].to);
64     return f[x][y]=tot/(du[y]+1)+1;
65 }
66  
67 int main(){
68 #ifndef ONLINE_JUDGE
69     freopen("1415.in","r",stdin);
70     freopen("1415.out","w",stdout);
71 #endif
72     memset(d,-1,sizeof d);
73     n=getint(); m=getint();
74     S=getint(); T=getint();
75     F(i,1,m){
76         int x=getint(),y=getint();
77         add(x,y);
78         du[x]++; du[y]++;
79     }
80     F(i,1,n) bfs(i);
81     printf("%.3f
",dp(S,T));
82     return 0;
83 }
84 
View Code

1415: [Noi2005]聪聪和可可

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 883  Solved: 528
[Submit][Status][Discuss]

Description

Input

数据的第1行为两个整数N和E,以空格分隔,分别表示森林中的景点数和连接相邻景点的路的条数。 第2行包含两个整数C和M,以空格分隔,分别表示初始时聪聪和可可所在的景点的编号。 接下来E行,每行两个整数,第i+2行的两个整数Ai和Bi表示景点Ai和景点Bi之间有一条路。 所有的路都是无向的,即:如果能从A走到B,就可以从B走到A。 输入保证任何两个景点之间不会有多于一条路直接相连,且聪聪和可可之间必有路直接或间接的相连。

Output

输出1个实数,四舍五入保留三位小数,表示平均多少个时间单位后聪聪会把可可吃掉。

Sample Input

【输入样例1】
4 3
1 4
1 2
2 3
3 4
【输入样例2】
9 9
9 3
1 2
2 3
3 4
4 5
3 6
4 6
4 7
7 8
8 9

Sample Output

【输出样例1】
1.500
【输出样例2】
2.167

HINT

【样例说明1】
开始时,聪聪和可可分别在景点1和景点4。
第一个时刻,聪聪先走,她向更靠近可可(景点4)的景点走动,走到景点2,然后走到景点3;假定忽略走路所花时间。
可可后走,有两种可能:
第一种是走到景点3,这样聪聪和可可到达同一个景点,可可被吃掉,步数为1,概率为 。
第二种是停在景点4,不被吃掉。概率为 。
到第二个时刻,聪聪向更靠近可可(景点4)的景点走动,只需要走一步即和可可在同一景点。因此这种情况下聪聪会在两步吃掉可可。
所以平均的步数是1* +2* =1.5步。


对于所有的数据,1≤N,E≤1000。
对于50%的数据,1≤N≤50。

Source

[Submit][Status][Discuss]
原文地址:https://www.cnblogs.com/Tunix/p/4570268.html