HDU 5001

http://acm.hdu.edu.cn/showproblem.php?pid=5001

每次去掉要算的点,求出到达其他点的概率,就是不能到达这个点的概率

开始想去算到达这个点的概率,再去减,不过这种方法证实是不对的,重复走的情况会重复计算

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std ;

double dp[55][10005] ;
int mp[55][55] ;
int cnt[55] ;
int main()
{
    int T ;
    scanf("%d",&T) ;
    while(T--)
    {
        int n,m,d ;
        scanf("%d%d%d",&n,&m,&d) ;
        memset(mp,0,sizeof(mp)) ;
        for(int i=0 ;i<m ;i++)
        {
            int a,b ;
            scanf("%d%d",&a,&b) ;
            mp[a][b]=mp[b][a]=1 ;
        }
        memset(cnt,0,sizeof(cnt)) ;
        for(int i=1 ;i<=n ;i++)
        {
            for(int j=1 ;j<=n ;j++)
            {
                if(i==j)continue ;
                if(mp[i][j])cnt[i]++ ;
            }
        }
        for(int v=1 ;v<=n ;v++)
        {
            for(int i=0 ;i<55 ;i++)
                for(int j=0 ;j<10005 ;j++)
                    dp[i][j]=0.0 ;
            for(int i=1 ;i<=n ;i++)
                dp[i][0]=1.0/n ;
            for(int i=1 ;i<=d ;i++)
            {
                for(int j=1 ;j<=n ;j++)
                {
                    if(v==j)continue ;
                    for(int h=1 ;h<=n ;h++)
                    {
                        if(mp[h][j])dp[h][i]+=dp[j][i-1]*(1.0/cnt[j]) ;
                    }
                }
            }
            double ans=0.0 ;
            for(int i=1 ;i<=n ;i++)
            {
                if(v==i)continue ;
                ans+=dp[i][d] ;
            }
            printf("%.6lf\n",ans) ;
        }
    }
    return 0 ;
}

时间: 2024-08-09 05:28:33

HDU 5001的相关文章

HDU 5001 Walk(鞍山网络赛E题)

HDU 5001 Walk 题目链接 思路:枚举每个要经过的点,然后进行状态转移,状态为dp[i][j],状态表示当前在j的点,已经走了i步,每次转移的时候,不从这个枚举的点出发,这样就可以求出所有路径经过该点的概率p, 然后1 - p就是不经过的答案 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; con

Hdu 5001 Walk 概率dp

Walk Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5001 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 bid

hdu 5001 Walk(概率)

http://acm.hdu.edu.cn/showproblem.php?pid=5001 应该算是一道简单的概率题.想了两个多小时,结果越想越麻烦.开了一个三维数组,MLE了.. 最后借鉴实验室学长的思路,发现这样想很直观,正退就可以. 设dp[j][d]表示不能经过i点走了d步到达j点的概率.那么dp[j][d] = ∑ dp[k][d-1]/edge[k].size().那么不经过i点的概率为∑dp[j][D]. #include <stdio.h> #include <iost

hdu 5001 概率DP 图上的DP

http://acm.hdu.edu.cn/showproblem.php?pid=5001 当时一看是图上的就跪了 不敢写,也没退出来DP方程 感觉区域赛的题  一则有一个点难以想到 二则就是编码有点难度. 这个题: 我一直的思路就是1-能到达i的概率 就是不能到达i的概率,然后三维方程巴拉巴拉,,,,把自己搞迷糊 正确做法: dp[k][j]   经过j步到达k点 并且不经过i点的概率 这么设的原因是,就可以求不能到达i点的概率了.   不能到达i点的概率就是segma(dp[v][j-1]

hdu 5001 walk 概率dp入门题

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 t

HDU 5001 Walk

解题思路:这是一道简单的概率dp,只要处理好相关的细节就可以了. dp[d][i]表示走d步时走到i的改概率,具体参考代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 #include<cstring> 6 using namespace std; 7 const int N = 10005; 8 const int M =

HDU 5001 Walk (暴力)

每次都去掉一个点求出到达 其他点的概率就是不能到达这个点的概率. Walk Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 51    Accepted Submission(s): 37 Special Judge Problem Description I used to think I could be anything, b

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

HDU 5001 Walk 求从任意点出发任意走不经过某个点的概率 概率dp 2014 ACM/ICPC Asia Regional Anshan Online

题意: 给定n个点m条边的无向图 问: 从任意点出发任意走d步,从不经过某个点的概率 dp[i][j]表示从不经过i点的前提下,走了d步到达j点的概率. #include <iostream> #include <cstdio> #include <string.h> #include <queue> #include <vector> #include <algorithm> #include <set> using n