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
     Recommend
    hujie
    题解:给你N个点,M条边(无向),然后可以走d步,让你求恰好不走i点(i 1~N)的概率 ;
    概率DP;dp[i][j]:表示走i步,到达j点的概率;对于当前点,它一定是由其相邻点走过来的,则这个点的概率为这些和他相邻的点的概率和 : dp[j][k]+=dp[j-1][vec[k][l]]*1.0/vec[k].size();
    然后依次枚举从每一个点,后面不可再出现这个点
    参考代码:
     1 // HDU5001 概率DP
     2 #include<bits/stdc++.h>
     3 using namespace std;
     4 #define PI acos(-1.0)
     5 #define eps 1e-8
     6 #define mem(a,b) memset(a,b,sizeof a)
     7 typedef long long LL;
     8 typedef pair<int,int> P;
     9 const int INF=0x3f3f3f3f;
    10 const LL inf=0x3f3f3f3f3f3f3f3fLL;
    11 const int maxn=2010;
    12 int T,n,m,d,tot,u,v,head[maxn];
    13 vector<int> vec[maxn];
    14 double dp[maxn*5][55];//dp[i][j]表示走i步恰好到j点的概率  
    15 int main()
    16 {
    17     ios::sync_with_stdio(false);
    18     cin>>T;
    19     while(T--)
    20     {
    21         cin>>n>>m>>d;
    22         for(int i=0;i<=n;i++) vec[i].clear();
    23         for(int i=1;i<=m;i++)
    24         {
    25             cin>>u>>v;
    26             vec[u].push_back(v);
    27             vec[v].push_back(u);
    28         }
    29         for(int i=1;i<=n;i++) dp[0][i]=1.0/n;
    30         for(int i=1;i<=n;i++)
    31         {
    32             for(int j=1;j<=d;j++)
    33             {
    34                 for(int k=1;k<=n;k++)
    35                 {
    36                     if(i==k) continue;
    37                     dp[j][k]=0.0;
    38                     for(int l=0;l<vec[k].size();l++)
    39                     {
    40                         if(i==vec[k][l]) continue;
    41                         dp[j][k]+=dp[j-1][vec[k][l]]*1.0/vec[k].size(); 
    42                     }
    43                 }
    44             }
    45             double ans=0.0;
    46             for(int j=1;j<=n;j++) if(j!=i) ans+=dp[d][j];
    47             cout<<setiosflags(ios::fixed)<<setprecision(6)<<ans<<endl;
    48         }    
    49     }
    50     return 0;
    51 }
    View Code

      

  • 相关阅读:
    Northwind数据库下载地址
    MSSQL跨服务访问数据库
    MSSQL基于一致性的I/O错误,解决方法之一
    DataGridView单元格ComboBox控件添加事件
    线程安全类 跨线程修改窗体UI
    数据库字段名
    SELECT INTO 和 INSERT INTO SELECT
    链表
    因为数据库正在使用,所以无法获得对数据库的独占访问权
    代替游标的循环
  • 原文地址:https://www.cnblogs.com/csushl/p/9651970.html
Copyright © 2011-2022 走看看