图论trainning-part-1 B. A Walk Through the Forest

B. A Walk Through the Forest

Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format: %I64d      Java class name: Main
 
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,B两点,如果统计d[A] > D[B]这种路径的条数。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #define LL long long
13 #define INF 0x3f3f3f
14 using namespace std;
15 const int maxn = 1010;
16 int mp[maxn][maxn],d[maxn],p[maxn];
17 int n,m;
18 bool vis[maxn];
19 void dij(int src){
20     int i,j,temp,index;
21     for(i = 1; i <= n; i++)
22         d[i] = INF;
23     d[src] = 0;
24     memset(vis,false,sizeof(vis));
25     for(i = 0; i < n; i++){
26         temp = INF;
27         for(j = 1; j <= n; j++)
28         if(!vis[j] && d[j] < temp) temp = d[index = j];
29         vis[index] = true;
30         for(j = 1; j <= n; j++)
31             if(!vis[j] && d[j] > d[index]+mp[index][j])
32                 d[j] = d[index] + mp[index][j];
33     }
34 }
35 int dfs(int s){
36     if(p[s]) return p[s];
37     if(s == 2) return 1;
38     int i,sum = 0;
39     for(i = 1; i <= n; i++){
40         if(mp[s][i] < INF && d[s] > d[i]) sum += dfs(i);
41     }
42     p[s]+= sum;
43     return p[s];
44 }
45 int main(){
46     int i,j,u,v,w;
47     while(scanf("%d",&n),n){
48         scanf("%d",&m);
49         for(i = 1; i <= n; i++)
50             for(j = 1; j <= n; j++)
51                 mp[i][j] = INF;
52         for(i = 0; i < m; i++){
53             scanf("%d%d%d",&u,&v,&w);
54             mp[u][v] = mp[v][u] = w;
55         }
56         dij(2);
57         memset(p,0,sizeof(p));
58         printf("%d\n",dfs(1));
59     }
60     return 0;
61 }
View Code

 

posted @ 2014-07-22 08:14  狂徒归来  阅读(234)  评论(0编辑  收藏  举报