HDOJ---1142 A Walk Through the Forest[DFS(记忆化搜索)+Dijkstra]

本博客讨论了计算在经过森林的特定路径时,Jimmy每天可以采取的不同路线数量的问题。通过使用记忆化搜索和Dijkstra算法,解决了这个问题并提供了详细的代码实现。

 

A Walk Through the Forest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3312    Accepted Submission(s): 1209


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
 

 

Source
 

 

Recommend
Eddy

 

 

 

 

 

记忆化搜索

code:

  1 #include <iostream>   
  2 #include <iomanip>   
  3 #include <fstream>   
  4 #include <sstream>   
  5 #include <algorithm>   
  6 #include <string>   
  7 #include <set>   
  8 #include <utility>   
  9 #include <queue>   
 10 #include <stack>   
 11 #include <list>   
 12 #include <vector>   
 13 #include <cstdio>   
 14 #include <cstdlib>   
 15 #include <cstring>   
 16 #include <cmath>   
 17 #include <ctime>   
 18 #include <ctype.h> 
 19 using namespace std;
 20 
 21 #define MAXN 1005
 22 #define intmax 9999999
 23 
 24 int map[MAXN][MAXN];
 25 int dis[MAXN];
 26 int sum[MAXN];
 27 int n,m;
 28 int cnt;
 29 
 30 void init()
 31 {
 32     int i,j;
 33     for(i=0;i<=n;i++)
 34         for(j=i;j<=n;j++)
 35             map[i][j]=map[j][i]=intmax;
 36 }
 37 
 38 void Dijkstra()
 39 {
 40     bool vst[MAXN];
 41     for(int i=1;i<=n;i++)
 42     {
 43         vst[i]=0;
 44         dis[i]=map[2][i];
 45     }
 46     dis[2]=0;
 47     vst[2]=1;
 48     for(int i=2;i<=n;i++)
 49     {
 50         int temp=intmax;
 51         int u=2;
 52         for(int j=1;j<=n;j++)
 53             if(dis[j]<temp&&!vst[j])
 54             {
 55                 temp=dis[j];
 56                 u=j;
 57             }
 58         vst[u]=1;
 59         for(int j=1;j<=n;j++)
 60         {
 61             if(map[u][j]<intmax)
 62             {
 63                 int update=dis[u]+map[u][j];
 64                 if(update<dis[j])
 65                     dis[j]=update;
 66             }
 67         }
 68     }
 69 }
 70 
 71 int dfs(int v)
 72 {
 73     int i;
 74     if(sum[v])                               //记忆化搜索
 75         return sum[v];                       //sum[i]表示从i到终点的所有可能路径
 76     if(v==2)
 77         return 1;
 78     for(i=1;i<=n;i++)
 79     {
 80         if(map[v][i]<intmax&&dis[i]<dis[v])
 81         {
 82             sum[v]+=dfs(i);
 83         }
 84     }
 85     return sum[v];
 86 }
 87 
 88 int main()
 89 {
 90     int i,j;
 91     int a,b,len;
 92     int dist;
 93     while(~scanf("%d",&n),n)
 94     {
 95         scanf("%d",&m);
 96         init();
 97         while(m--)
 98         {
 99             scanf("%d%d%d",&a,&b,&len);
100             if(map[a][b]>len)
101                 map[a][b]=map[b][a]=len;
102         }
103         Dijkstra();
104         memset(sum,0,sizeof(sum));
105         printf("%d\n",dfs(1));
106     }
107     return 0;
108 }

 

转载于:https://www.cnblogs.com/XBWer/archive/2012/08/10/2631797.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值