zoukankan      html  css  js  c++  java
  • 【BZOJ1415】 [Noi2005]聪聪和可可 概率与期望

    其实题不难,不知提交了几次。。。不能代码MD。。。注意一些基本问题。。。SB概率题

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #define N 1001
     5 using namespace std;
     6 double f[N][N];
     7 int n,m,cnt,a,b;
     8 struct E{int next,to;}e[2*N+10];
     9 int head[N],ds[N],q[N],dis[N][N],p[N][N];
    10 inline int read()
    11 {
    12     int ans=0,f=1;
    13     char c;
    14     while (!isdigit(c=getchar())) if (c=='-') f=-1;
    15     ans=c-'0';
    16     while (isdigit(c=getchar())) ans=ans*10+c-'0';
    17     return ans*f;
    18 }
    19 void insert(int u,int v)
    20 {
    21     cnt++; e[cnt].next=head[u]; head[u]=cnt; e[cnt].to=v; ds[u]++;
    22     cnt++; e[cnt].next=head[v]; head[v]=cnt; e[cnt].to=u; ds[v]++;
    23 }
    24 double DP(int x,int y)
    25 {
    26     if (f[x][y]) return f[x][y];
    27     if (x==y) return 0;
    28     if (p[x][y]==y || p[p[x][y]][y]==y) return f[x][y]=1;
    29     double tot=DP(p[p[x][y]][y],y);
    30     for (int i=head[y];i;i=e[i].next)
    31         tot+=DP(p[p[x][y]][y],e[i].to);
    32     return f[x][y]=tot/(ds[y]+1)+1; 
    33 }
    34 void Bfs(int x)
    35 {
    36     int t=0,w=1;
    37     q[t]=x;
    38     dis[x][x]=0;    
    39     while (t!=w)
    40     {
    41         int now=q[t],tmp=p[x][now];
    42         t++; if (t==1001) t=0;
    43         for (int i=head[now];i;i=e[i].next)
    44             if (dis[x][e[i].to]==-1 || (dis[x][e[i].to]==dis[x][now]+1 && tmp<p[x][e[i].to]))
    45             {
    46                 dis[x][e[i].to]=dis[x][now]+1;
    47                 p[x][e[i].to]=tmp;
    48                 if (!tmp) p[x][e[i].to]=e[i].to;
    49                 q[w]=e[i].to;
    50                 w++; if (w==1001) w=0;
    51             }
    52     }
    53 } 
    54 int main()
    55 {
    56     memset(dis,-1,sizeof(dis));
    57     n=read(); m=read(); a=read(); b=read();
    58     for (int i=1;i<=m;i++)
    59     {
    60         int u,v;
    61         u=read(); v=read();
    62         insert(u,v);
    63     }
    64     for (int i=1;i<=n;i++) Bfs(i);
    65     printf("%.3lf",DP(a,b));
    66     return 0;
    67 }
    View Code

    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

    —Anime Otaku Save The World.
  • 相关阅读:
    安装虚拟环境virtualenv
    安装python3、ipython、jupyter
    配置yum源
    面向对象
    sqrt开平方算法的尝试,是的看了卡马克大叔的代码,我来试试用C#写个0x5f3759df和0x5f375a86跟System.Math.Sqrt到底哪个更强
    python开发环境
    phthon中的open函数模式
    picoscope 动态链接库调用位置确定,可进行图标编辑
    设计模式笔记(2)-工厂模式
    设计模式笔记(1)-单体模式
  • 原文地址:https://www.cnblogs.com/DMoon/p/5350306.html
Copyright © 2011-2022 走看看