hdu1142 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): 3018    Accepted Submission(s): 1101


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
 
題意:看樣子很多人都把這題目看錯了,以為是求最短路的條數。真正的意思是:假設 A 和 B 是相連的,當前在 A 處,如果 A 到終點的距離大於 B 到終點的距離,
則可以從 A 通往 B 處,問滿足這種的條件的路徑條數。
分析:1、以終點 2 為起點 dijkstra;
     2、直接DFS記憶化搜索。
 
View Code
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string.h>
 4 #define N 1010
 5 #define INF 2000000000
 6 
 7 using namespace std;
 8 
 9 int map[N][N],lowcost[N],visited[N],d[N],p[N];
10 
11 
12 void dijkstra(int s,int n)
13 {
14     memset(visited,false,sizeof(visited));
15     int i,j,k,min;
16     for(i=1;i<=n;i++)
17     {
18         lowcost[i]=map[s][i];
19     }
20     d[s]=0;
21     visited[s]=true;
22     for(i=1;i<n;i++)//我又寫成 i<=n 了 調試很久!!!
23     {
24         min=INF;
25         for(j=1;j<=n;j++)
26         {
27             if(!visited[j]&&min>lowcost[j])
28             {
29                 min=lowcost[j];
30                 k=j;
31             }
32         }
33         d[k]=min;
34         visited[k]=true;
35         for(j=1;j<=n;j++)
36         {
37             if(!visited[j]&&lowcost[j]>map[k][j]+d[k])
38                 lowcost[j]=map[k][j]+d[k];
39         }
40     }
41 }
42 
43 int DFS(int s,int n)
44 {
45     if(p[s]) return p[s];
46     if(s==2)  return 1;
47     int i,sum=0;
48     for(i=1;i<=n;i++)
49     {
50         if(map[s][i]<INF&&d[s]>d[i])
51         {
52             if(p[i]) sum=sum+p[i];
53             else sum=sum+DFS(i,n);
54         }
55     }
56     sum=sum+p[s];
57     p[s]=sum;
58     return p[s];
59 }
60 
61 int main()
62 {
63     int i,j,n,m,u,v,w;
64     while(cin>>n&&n)
65     {
66         cin>>m;
67         memset(p,0,sizeof(p));
68         for(i=1;i<=n;i++)
69         {
70             for(j=1;j<=n;j++)
71             {
72                 map[i][j]=(i==j?0:INF);
73             }
74         }
75         for(i=0;i<m;i++)
76         {
77             scanf("%d%d%d",&u,&v,&w);
78             map[u][v]=map[v][u]=w;
79         }
80         dijkstra(2,n);
81         cout<<DFS(1,n)<<endl;
82     }
83     return 0;
84 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM