zoukankan      html  css  js  c++  java
  • JZOJ 3388. 【NOIP2013模拟】绿豆蛙的归宿

    Description

    随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿。

    给出一个有向无环图,起点为1终点为N,每条边都有一个长度,并且从起点出发能够到达所有的点,所有的点也都能够到达终点。绿豆蛙从起点出发,走向终点。

    到达每一个顶点时,如果有K条离开该点的道路,绿豆蛙可以选择任意一条道路离开该点,并且走向每条路的概率为 1/K 。

    现在绿豆蛙想知道,从起点走到终点的所经过的路径总长度期望是多少?
     

    Input

    第一行: 两个整数 N M,代表图中有N个点、M条边

    第二行到第 1+M 行: 每行3个整数 a b c,代表从a到b有一条长度为c的有向边

    Output

    从起点到终点路径总长度的期望值,四舍五入保留两位小数。
     

    Sample Input

    4 4
    1 2 1
    1 3 2
    2 3 3
    3 4 4

    Sample Output

    7.00
     

    Data Constraint

    对于20%的数据   N<=100

    对于40%的数据   N<=1000

    对于60%的数据   N<=10000

    对于100%的数据  N<=100000,M<=2*N
     
    做法:仔细观察可以发现,这题跟所谓的期望其实没多大关系。。用暴力算答案的复杂度为O(M), 所以搜索即可。
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #define N 100007
     5 using namespace std;
     6 struct edge
     7 {
     8     int to, next;
     9     double w;
    10 }e[2 * N];
    11 int n, m, ls[N], tot, cd[N], rd[N];
    12 double dis[N];
    13 
    14 void add(int x, int y, double z)
    15 {
    16     e[++tot].to = y;
    17     e[tot].w = z;
    18     e[tot].next = ls[x];
    19     ls[x] = tot;
    20 }
    21 
    22 void dfs(int x, double s, double tot)
    23 {
    24     if (x == n)
    25     {
    26         dis[n] += tot * s;
    27         return;
    28     }    
    29     s *= (1.0 / (double)cd[x]);
    30     for (int i = ls[x]; i; i = e[i].next)
    31     {
    32         tot += e[i].w;
    33         dfs(e[i].to, s, tot);
    34         tot -= e[i].w;
    35     }
    36 }
    37 
    38 int main()
    39 {
    40     scanf("%d%d", &n, &m);
    41     int x, y;
    42     double z;
    43     for (int i = 1; i <= m; i++)
    44     {
    45         scanf("%d%d", &x, &y);
    46         scanf("%lf", &z);
    47         cd[x]++;
    48         add(x, y, z);
    49     }
    50     dfs(1, 1.0, 0);
    51     printf("%.2f", dis[n]);
    52 }
    View Code
  • 相关阅读:
    工作量单位-人月、人日、人时 详解
    PHP数组的交集array_intersect(),array_intersect_assoc(),array_inter_key()函数详解
    常用的Mysql数据库操作语句大全
    Linux服务器,PHP的10大安全配置实践
    PHP如何获取二个日期的相差天数?
    常见HTTP状态码列表
    PHP中静态(static)调用非静态方法详解
    PHP引用(&)初探:函数的引用返回
    PHP的大括号(花括号{})使用详解
    详解JavaScript中的Url编码/解码,表单提交中网址编码
  • 原文地址:https://www.cnblogs.com/traveller-ly/p/9427069.html
Copyright © 2011-2022 走看看