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

Posted 霜雪千年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了题解报告: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, 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
解题思路:从起点1到终点2,如果从a点到2的距离大于从b点到2的距离,并且a能到b(即两点间有边(路)),那么就从a走到b。问这样的路有几条。Dijkstra+记忆化搜索!
AC代码:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int INF = 0x3f3f3f3f;
 4 const int MAXN = 1005;
 5 int n,m,a,b,c,dis[MAXN],pis[MAXN],G[MAXN][MAXN];
 6 bool vis[MAXN];
 7 void Dijkstra()
 8 {
 9     for(int i=1;i<=n;i++)//把终点2当成起点,求出起点2到各节点的最短路径
10         dis[i]=G[2][i];
11      dis[2]=0;vis[2]=true;
12      for(int i=1;i<n;i++){
13         int k=-1;
14         for(int j=1;j<=n;j++)
15             if(!vis[j] && (k==-1||dis[j]<dis[k]))k=j;
16         if(k==-1)break;
17         vis[k]=true;
18         for(int j=1;j<=n;j++)
19              if(!vis[j])dis[j]=min(dis[j],dis[k]+G[k][j]);
20     }
21 }
22 int dfs(int x){//记忆化搜索
23     if(pis[x]!=-1)return pis[x];
24     if(x==2)return 1;
25     pis[x]=0;
26     for(int i=1;i<=n;++i)//x到i有路且x到终点距离大于i到终点距离
27         if(G[x][i]!=INF && dis[x]>dis[i])pis[x]+=dfs(i);
28     return pis[x];
29 }
30 int main()
31 {
32     while(~scanf("%d",&n) && n){
33         scanf("%d",&m);
34         for(int i=1;i<=n;++i)
35             for(int j=1;j<=n;++j)
36                 G[i][j]=(i==j?0:INF);
37         memset(vis,false,sizeof(vis));
38         memset(pis,-1,sizeof(pis));
39         for(int i=1;i<=m;++i){
40             scanf("%d %d %d",&a,&b,&c);
41             G[a][b]=G[b][a]=c;
42         }
43         Dijkstra();
44         printf("%d\n",dfs(1));
45     }
46     return 0;
47 }

 

以上是关于题解报告:hdu 1142 A Walk Through the Forest的主要内容,如果未能解决你的问题,请参考以下文章

HDU1142 A Walk Through the Forest(最短路+DAG)

HDU - 1142 A Walk Through the Forest(Dijkstra+DFS)

HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)

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

HDU 1142 A Walk Through the Forest

HDU 1142 A Walk Through the Forest锛堟渶鐭矾+璁板繂鍖栨悳绱級