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): 6045    Accepted Submission(s): 2234


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 处,问满足这种的条件的路径条数。 ps.本来没看懂的,一直wa知道看了大牛博客才知道问题
dfs+dijkstra
 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 #define M 1010
 5 #define inf 99999999
 6 int map[M][M],dis[M];//定义map,和dijkstra数组
 7 bool vid[M];//dijkstra标记数组
 8 int dp[M];//记忆搜索
 9 int main()
10 {
11     int m,n,i,j;
12     int dfs(int s,int n);
13     void dijkstra(int n);
14     while(cin>>n,n)
15     {
16         cin>>m;
17         int a,b,c;
18         for(i=1;i<=n;i++)
19             for(j=1;j<=n;j++)
20                 map[i][j] = i==j? 0:inf;//初始化
21         while(m--)
22         {
23             cin>>a>>b>>c;
24             if(map[a][b]>c)
25                 map[a][b] = map[b][a] = c;
26         }
27         dijkstra(n);
28         memset(vid,0,sizeof(vid));
29         memset(dp,-1,sizeof(dp));
30         cout<<dfs(1,n)<<endl;
31     }
32     return 0;
33 }
34 void dijkstra(int n)
35 {
36     int i,j,v;
37     vid[2]=1;
38     for(i=1;i<=n;i++)
39         dis[i]=map[2][i];
40     j=1;
41     while(1)
42     {
43         v=0;
44         for(i=1;i<=n;i++)
45             if(!vid[i]&&(v==0||dis[v]>dis[i]))
46                 v=i;
47         if(!v) break;
48         vid[v]=1;
49         for(i=1;i<=n;i++)
50             dis[i]=min(dis[i],dis[v]+map[v][i]);
51     }
52 }//dijkstra算法直接模板记得记录终点到其他点的距离
53 int dfs(int s,int n)
54 {
55     if(dp[s]!=-1)
56         return dp[s];
57     if(s==2)
58         return 1;
59     dp[s]=0;
60     for(int i=1;i<=n;i++)
61         if(dis[i]<dis[s]&&map[i][s]!=inf)
62             dp[s]+=dfs(i,n);
63     return dp[s];
64 }//记忆搜索,没看怎么懂就不乱说了

 

posted @ 2015-01-29 15:45  乱齐八糟  阅读(152)  评论(0)    收藏  举报