HDU5001-Walk(记忆化搜索)

题目链接

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

思路:记忆化搜索,枚举每个点a,dp[i][j]表示走了i步到达j点的概率(不包括a点),注意初始化清空。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 55;
const int MAXM = 10005;

vector<int> p[MAXN];
double dp[MAXM][MAXN];

int n, m, d;

void init() {
    for (int i = 0; i <= n; i++)
        p[i].clear();
}

double solve(int a) {

    memset(dp, 0, sizeof(dp));
    double ans = 0;
    dp[0][0] = 1;

    for (int i = 0; i <= d; i++) {
        for (int j = 0; j <= n; j++) {
            if (j == a)
                continue;
            double b = 1.0 / p[j].size();
            for (int k = 0; k < p[j].size(); k++)
                dp[i + 1][p[j][k]] += dp[i][j] * b;
        }
        ans += dp[i + 1][a];
    }
    return 1.0 - ans;
}

int main() {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d%d%d", &n, &m, &d);
        init();
        int x, y;
        for (int i = 0; i < m; i++) {
            scanf("%d%d", &x, &y);
            p[x].push_back(y);
            p[y].push_back(x);
        }
        for (int i = 1; i <= n; i++)
            p[0].push_back(i);

        for (int i = 1; i <= n; i++) {
            double ans = solve(i);
            printf("%.10lf\n", ans);
        }
    }
    return 0;
}
时间: 2024-10-19 03:44:14

HDU5001-Walk(记忆化搜索)的相关文章

HDU 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索 好题)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6350    Accepted Submission(s): 2332 Problem Description Jimmy experiences a lot of stress at work these days, especial

hdu 1142 A Walk Through the Forest (digkstra+记忆化搜索)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7322    Accepted Submission(s): 2685 Problem Description Jimmy experiences a lot of stress at work these days, especial

HDU--1142--A Walk Through the Forest--深广搜/DP/最短路径/记忆化搜索

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5948    Accepted Submission(s): 2191 Problem Description Jimmy experiences a lot of stress at work these days, especial

HDU1142 A Walk Through the Forest 【SPFA】+【记忆化搜索】

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5688    Accepted Submission(s): 2089 Problem Description Jimmy experiences a lot of stress at work these days, especial

hdu 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5984    Accepted Submission(s): 2211 Problem Description Jimmy experiences a lot of stress at work these days, especial

HDU 1142 A Walk Through the Forest (记忆化搜索+Dijkstra算法)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7583    Accepted Submission(s): 2791 Problem Description Jimmy experiences a lot of stress at work these days, especial

hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5679    Accepted Submission(s): 2086 Problem Description Jimmy experiences a lot of stress at work these days, especiall

HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10172    Accepted Submission(s): 3701 Problem Description Jimmy experiences a lot of stress at work these days, especial

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 1142(迪杰斯特拉+记忆化搜索)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7330    Accepted Submission(s): 2687 Problem Description Jimmy experiences a lot of stress at work these days, especiall