HDU - 1142 A Walk Through the Forest (DP + 最短路)

题目大意:有一个人工作完了,要回家了。家在节点2,办公室在节点1。如果选择A回家的最短路比选择B回家的最短路小,那么他就可以走A点回家,问这个人有多少种回家的方法

解题思路:先跑一遍最短路,求出每个节点到家的最短距离,然后进行判断

设dp[i]为从i点到家有多少种方法,如果d[i] > d[j](d数组表示到家的最短距离)

那么dp[i] += dp[j]

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define N 1010
#define INF 0x3f3f3f3f
int dis[N][N], d[N], dp[N];
bool vis[N];
int n, m;
vector<int> v[N];

void init() {

    memset(dis, 0x3f, sizeof(dis));
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i <= n; i++)
        v[i].clear();

    int x, y, z;
    for (int i = 0; i < m; i++) {
        scanf("%d%d%d", &x, &y, &z);
        dis[x][y] = dis[y][x] = z;
        v[x].push_back(y);
        v[y].push_back(x);
    }
}

void SPFA() {
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; i++)
        d[i] = INF;
    queue<int> q;
    d[2] = 0;
    vis[2] = true;
    q.push(2);

    while (!q.empty()) {
        int t = q.front();
        q.pop();
        vis[t] = 0;

        for (int i = 0; i < v[t].size(); i++) {
            if (d[v[t][i]] > d[t] + dis[t][v[t][i]]) {
                d[v[t][i]] = d[t] + dis[t][v[t][i]];
                if (!vis[v[t][i]]) {
                    vis[v[t][i]] = 1;
                    q.push(v[t][i]);
                }
            }
        }
    }
}

int dfs(int cur) {
    if (cur == 2)
        return 1;
    if (dp[cur])
        return dp[cur];

    for (int i = 0; i < v[cur].size(); i++) {
        if (d[cur] > d[v[cur][i]]) {
            dp[cur] += dfs(v[cur][i]);
        }
    }
    return dp[cur];
}

int main() {
    while (scanf("%d", &n) != EOF && n) {
        scanf("%d", &m);
        init();
        SPFA();
        printf("%d\n", dfs(1));
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-22 07:26:46

HDU - 1142 A Walk Through the Forest (DP + 最短路)的相关文章

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 1142 A Walk Through the Forest(dijkstra+记忆化DFS)

题意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路.问满足这样的路径条数 有多少,噶呜~~题意是搜了解题报告才明白的Orz....英语渣~ 思路: 1.1为起点,2为终点,因为要走ab路时,必须保证那个条件,所以从终点开始使用单源最短路Dijkstra算法,得到每个点到终点的最短路,保存在dis[]数组中. 2.然后从起点开始深搜每条路,看看满足题意的路径有多少条. 3.这样搜索之后,dp[1]就是从起

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 (记忆化搜索+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

题解报告:hdu 1142 A Walk Through the Forest

题目链接:acm.hdu.edu.cn/showproblem.php?pid=1142 Problem Description Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer,

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【记忆化搜索+最短路Dijkstra算法】

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

hdu 1142 A Walk Through the Forest (最短路+dfs )

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

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): 5306    Accepted Submission(s): 1939 Problem Description Jimmy experiences a lot of stress at work these days, especiall

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