HDU 5001-Walk(概率dp)

题意:

给你一个图,求在长度为d的所有路径,不经过每个结点的概率

分析:

枚举每个结点,正推求概率

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
vector<int>e[55];
int n,m,d;
double dp[55][10010];
void solve(){
    for(int i=1;i<=n;++i){
        memset(dp,0,sizeof(dp));
        for(int p=1;p<=n;++p)
            dp[p][0]=1.0/n;
        for(int j=0;j<d;++j){
            for(int k=1;k<=n;++k){
                if(k==i)continue;
                for(int l=0;l<e[k].size();++l){
                    if(e[k][l]==i)continue;
                    dp[e[k][l]][j+1]+=dp[k][j]*1.0/e[k].size();
                }
            }
        }
        double total=0.0;
        for(int j=1;j<=n;++j)
            if(j!=i)
            total+=dp[j][d];
        printf("%.10lf\n",total);
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d",&n,&m,&d);
        int u,v;
        for(int i=1;i<=n;++i)
            e[i].clear();
        while(m--){
            scanf("%d%d",&u,&v);
            e[u].push_back(v);
            e[v].push_back(u);
        }
        solve();
    }
return 0;
}
时间: 2024-08-21 14:33:57

HDU 5001-Walk(概率dp)的相关文章

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

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 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 4870 Rating(概率DP&amp;高数消元)

Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 714    Accepted Submission(s): 452 Special Judge Problem Description A little girl loves programming competition very much. Recently, she

HDU 4035Maze(概率DP)

HDU 4035   Maze 体会到了状态转移,化简方程的重要性 题解转自http://blog.csdn.net/morgan_xww/article/details/6776947 /** dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点1处(概率为ki) 2.找到出口,走出迷宫 (概率为ei) 3.和该点相连有m条边,随机走一条 求:走出迷宫所要走的边数的期望值. 设 E[i]表示

HDU - 5001 Walk(概率dp+记忆化搜索)

Walk 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 ad

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

[概率dp] hdu 5001 Walk

题意:n个点(1~n),m条边,走k次,双向边,每次随机走一条路,起点也随机,问不走到各个点的概率是多少. 思路: 概率dp[i][j][k] 前i步 走到j 走不到k的概率. 那么状态转移就是 j能走到的点,传递所有dp[i][j][k]的值乘上概率. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"

HDU5001 Walk(概率DP)

A - Walk Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 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 loo