zoukankan      html  css  js  c++  java
  • hdu 5001 Walk(概率dp)

    Problem Description
    I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling.
    
    The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel to an adjacent node with the same probability in the next step. I will pick up the start node randomly (each node in the graph has the same probability.), and travel for d steps, noting that I may go through some nodes multiple times.
    
    If I miss some sights at a node, it will make me unhappy. So I wonder for each node, what is the probability that my path doesn't contain it.
    Input
    The first line contains an integer T, denoting the number of the test cases.
    
    For each test case, the first line contains 3 integers n, m and d, denoting the number of vertices, the number of edges and the number of steps respectively. Then m lines follows, each containing two integers a and b, denoting there is an edge between node a and node b.
    
    T<=20, n<=50, n-1<=m<=n*(n-1)/2, 1<=d<=10000. There is no self-loops or multiple edges in the graph, and the graph is connected. The nodes are indexed from 1.
     
    Output
    For each test cases, output n lines, the i-th line containing the desired probability for the i-th node.
    
    Your answer will be accepted if its absolute error doesn't exceed 1e-5.
     
    Sample Input
    2
    5 10 100
    1 2
    2 3
    3 4
    4 5
    1 5
    2 4
    3 5
    2 5
    1 4
    1 3
    10 10 10
    1 2
    2 3
    3 4
    4 5
    5 6
    6 7
    7 8
    8 9
    9 10
    4 9
     
    Sample Output
    0.0000000000
    0.0000000000
    0.0000000000
    0.0000000000
    0.0000000000
    0.6993317967
    0.5864284952
    0.4440860821
    0.2275896991
    0.4294074591
    0.4851048742
    0.4896018842
    0.4525044250
    0.3406567483
    0.6421630037
     
    Source
     
     

     题意:一张无向图,要你求出走d步之后,每个点无法到达的概率。

    dp[i][j]表示走了i步时,到达点j的概率

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)  
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define MOD 1000000007
    22 #define N 56
    23 #define M 10006
    24 #define inf 1e12
    25 int n,m,d;
    26 vector<int> g[N];
    27 double dp[M][N];
    28 int main()
    29 {
    30     int t;
    31     scanf("%d",&t);
    32     while(t--){
    33         for(int i=0;i<N;i++){
    34             g[i].clear();
    35         }
    36         scanf("%d%d%d",&n,&m,&d);
    37         for(int i=0;i<m;i++){
    38             int u,v;
    39             scanf("%d%d",&u,&v);
    40             g[u].push_back(v);
    41             g[v].push_back(u);
    42         }
    43         
    44         for(int i=1;i<=n;i++){
    45             dp[0][i]=1.0/n;
    46         }
    47         
    48         for(int l=1;l<=n;l++){
    49             for(int i=1;i<=d;i++){
    50                 for(int j=0;j<=n;j++){
    51                     dp[i][j]=0;
    52                 }
    53             }
    54             for(int i=1;i<=d;i++){
    55                 for(int j=1;j<=n;j++){
    56                     if(l==j){
    57                         continue;
    58                     }
    59                     int len=g[j].size();
    60                     for(int k=0;k<len;k++){
    61                         if(g[j][k]==l){
    62                             continue;
    63                         }
    64                         dp[i][g[j][k]]+=dp[i-1][j]/len;
    65                     }
    66                 }
    67             }
    68             double ans=0;
    69             for(int i=1;i<=n;i++){
    70                 ans+=dp[d][i];
    71             }
    72             printf("%.10lf
    ",ans);
    73         }
    74         
    75     }
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    2017-2018-1 20155301 20155307 20155339 《信息安全系统设计基础》 实验一 开发环境的熟悉
    《JAVA程序设计》第八周学习总结
    实验二《Java面向对象程序设计》实验报告
    《JAVA程序设计》第七周学习总结
    《JAVA程序设计》结对编程联系_四则运算(第二周:整体性总结)
    《JAVA程序设计》第六周学习总结
    实验一《Java开发环境的熟悉》实验报告
    《JAVA程序设计》结对编程联系_四则运算(第一周:阶段总结)
    《JAVA程序设计》第五周学习总结
    《JAVA程序设计》第四周学习总结
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4827329.html
Copyright © 2011-2022 走看看