zoukankan      html  css  js  c++  java
  • HDU 5001 概率dp

                                                         Walk

                                                                Time Limit : 30000/15000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
    Special Judge

    Font: Times New Roman | Verdana | Georgia

    Font Size: ← →

    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

    2014 ACM/ICPC Asia Regional Anshan Online
     
    题意:给你一个无向图,可以从任意点出发问你走d步的特定下,这n个点分别不能经过的概率
     
    上代码:
    ///1085422276
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<queue>
    #include<cmath>
    #include<map>
    #include<bitset>
    #include<set>
    #include<vector>
    using namespace std ;
    typedef long long ll;
    #define mem(a) memset(a,0,sizeof(a))
    #define TS printf("111111
    ");
    #define FOR(i,a,b) for( int i=a;i<=b;i++)
    #define FORJ(i,a,b) for(int i=a;i>=b;i--)
    #define READ(a,b) scanf("%d%d",&a,&b)
    #define mod 1000000007
    #define inf 100000
    #define maxn 300000
    inline ll read()
    {
        ll x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*f;
    }
    //******************************************************************
    double dp[51][10005];
    int n,m,D,vis[10005],jh;
    vector<int >G[500];
    void dfs(int x,int d)
    {
        if(x==jh)return;
        int tmp=G[x].size();
       for(int i=0;i<tmp;i++)
       {
        dp[G[x][i]][d]+=dp[x][d-1]/(tmp);
       }
    }
    int main()
    {
    
        int T=read();
        while(T--)
        {
            n=read(),m=read(),D=read();
            mem(dp);
            int a,b;
            FOR(i,1,n)G[i].clear();
            FOR(i,1,m)
            {
                READ(a,b);
                G[a].push_back(b);
                G[b].push_back(a);
            }
            FOR(i,1,n){
            mem(dp);   jh=i;
            FOR(j,1,n) dp[j][0]=1.0/n;
            FOR(j,1,D) FOR(k,1,n)dfs(k,j);
            double ans=0.0;
            FOR(j,0,D)ans+=dp[i][j];
            printf("%.10f
    ",1-ans);
            }
        }
        return 0;
    }
    代码
  • 相关阅读:
    学习JavaScript 的必备 (一),让您对js的 function, javascript内置对象,this概念及之间的关系不再迷惑。(希望能置为推荐篇,为更多的js初学者关注)
    你人脉网中应该有的10种人
    十句话,不黄不色,但很经典~~~~~~~~~~
    js网站
    …↓爆》笑
    JavaScript使用技巧精萃
    JDK源码分析之Set类详解——适配器模式的应用
    好的程序员做不出好的软件设计
    JDK源码中ClassLoader的浅析
    提高编程技巧的十大方法
  • 原文地址:https://www.cnblogs.com/zxhl/p/4782249.html
Copyright © 2011-2022 走看看