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, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A
nice walk through the forest, seeing the birds and chipmunks is quite enjoyable.

The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from
B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.

Input

Input contains several test cases followed by a line containing 0. Jimmy has numbered each intersection or joining of paths starting with 1. His office is numbered 1, and his house is numbered 2. The first line of each test case gives the number of intersections
N, 1 < N ≤ 1000, and the number of paths M. The following M lines each contain a pair of intersections a b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between intersection a and a different intersection b. Jimmy may walk a path any
direction he chooses. There is at most one path between any pair of intersections.

Output

For each test case, output a single integer indicating the number of different routes through the forest. You may assume that this number does not exceed 2147483647

Sample Input

5 6
1 3 2
1 4 2
3 4 3
1 5 12
4 2 34
5 2 24
7 8
1 3 1
1 4 1
3 7 1
7 4 1
7 5 1
6 7 1
5 2 1
6 2 1
0

Sample Output

2
4
#include <iostream>
#include <cstring>
#include<stdio.h>
//#define INT 1000000000+5 如果 2*INT  2*1000000000+5则值是2000000005
using namespace std;
const int INT=0x3f3f3f3f;//定义常量,最好用const ,本题必须用16进制 用10进制表示INT 会出错 我也不知道为什么
const int maxn=1005;
int dist[maxn];
int map[maxn][maxn];
bool vis[maxn];
int dp[maxn];
int dian,side;
void Dijkstra(int x)
{
    for(int i=1;i<=dian;i++)
    {
        dist[i]=map[x][i];//点2到各个点的距离记录到dist[i]中
    }
    memset(vis,false,sizeof(vis));//标记所有是没走过
    vis[x]=true;
    dist[x]=0;//一定要确定这一步
    int index;
    for(int i=1;i<=dian;i++)
    {
        int minn=INT;
        for(int ii=1;ii<=dian;ii++)//遍历所有点,找出第一个到2的最小距路径 标记给index
        {
            if(!vis[ii]&&dist[ii]<minn)//也就是所有2能到达的点中,index到2的距离最短
            {
                minn=dist[ii];
                index=ii;
            }
        }
        if(minn==INT) break;//如果所有的点都无法到达 2 退出循环
        vis[index]=true;
        for(int j=1;j<=dian;j++)//更新一遍所有的点到2的距离
        {
            if(!vis[j]&&dist[index]+map[index][j]<dist[j])//没标记过 && 符合题意的条件
            {
                dist[j]=dist[index]+map[index][j];
            }
        }
    }
}
int DFS(int x)
{
    int sum=0;
    if(dp[x]) return dp[x];

    for(int i=1;i<=dian;i++)
    {
        if(map[x][i]!=INT&&dist[x]>dist[i])//(每次搜索一条能走的路&&下一个点i到2的距离要小于这个点到2的距离)
        {
            //printf("i=%d  \n",i);
            sum+=DFS(i);
            //printf("%d\n",i);
        }
    }
    return dp[x]=sum;
}
int main (void)
{
    int a,b;
    int far;
    while(~scanf("%d",&dian),dian)
    {
        scanf("%d",&side);
        memset(map,INT,sizeof(map));
        for(int i=1; i<=side; i++)
        {
            scanf("%d%d%d",&a,&b,&far);
            map[a][b]=map[b][a]=far;
        }
        Dijkstra(2);
        /* for(int i=1;i<=dian;i++)
        {
            printf("i=%d dist=%d\n",i,dist[i]);
        }*/
        memset(dp,0,sizeof(dp));
        dp[2]=1;
        printf("%d\n",DFS(1));
    }
    return 0;
}
时间: 2024-10-11 12:59:51

HDU 1142 A Walk Through the Forest【记忆化搜索+最短路Dijkstra算法】的相关文章

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 (DP + 最短路)

题目大意:有一个人工作完了,要回家了.家在节点2,办公室在节点1.如果选择A回家的最短路比选择B回家的最短路小,那么他就可以走A点回家,问这个人有多少种回家的方法 解题思路:先跑一遍最短路,求出每个节点到家的最短距离,然后进行判断 设dp[i]为从i点到家有多少种方法,如果d[i] > d[j](d数组表示到家的最短距离) 那么dp[i] += dp[j] #include <cstdio> #include <cstring> #include <algorithm&

HDU 1248 漫步校园【记忆化搜索+优先队列+最短路径dijkstra算法综合运用】

漫步校园 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 24   Accepted Submission(s) : 8 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于长时间坐在

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 (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): 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(最短路+记忆化搜索)

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