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): 4842    Accepted Submission(s): 1758


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   |   We have carefully selected several similar problems for you:  1301 1598 2722 2962 2923 
 

 

  给出spfa和dij解法

spfa:

 1 //15MS    768K    1569 B    C++    
 2 /*
 3     
 4     题意: 
 5         开始以为是求最短路的线路数,最后发现错了- -
 6     其实是求这样的线路数:1到2的过程中经过的路AB,A到2比B到2远
 7     
 8     最短路径:
 9         泪奔...先用SPFA将终点2到其他点的最短距离搜出,然后
10     在记忆化搜索求出符合条件的路径数 
11       
12 */
13 #include<iostream>
14 #include<queue>
15 #include<vector>
16 #define inf 0x7fffffff
17 using namespace std;
18 struct node{
19     int v;
20     int w;
21     node(int a,int b){
22         v=a; w=b;
23     }
24 };
25 int vis[1005];
26 int d[1005];
27 vector<node>V[10005];
28 int ans[1005];
29 int n,m;
30 void spfa(int s)
31 {
32     memset(vis,0,sizeof(vis));
33     for(int i=0;i<=n;i++)
34         d[i]=inf;
35     d[s]=0;
36     vis[s]=1;
37     queue<int>Q;
38     Q.push(s);
39     while(!Q.empty()){
40         int u=Q.front();
41         Q.pop();
42         vis[u]=0;
43         int n0=V[u].size();
44         for(int i=0;i<n0;i++){
45             int v=V[u][i].v;
46             int w=V[u][i].w;
47             if(d[v]>d[u]+w){
48                 d[v]=d[u]+w;
49                 //if(v==2 && d[v]==minn) cnt++;
50                 if(!vis[v]){
51                     vis[v]=1;
52                     Q.push(v);
53                 }
54             }
55         }
56     }   
57 }
58 int dfs(int u)
59 {
60     if(u==2) return 1;
61     if(ans[u]>0) return ans[u];
62     int n0=V[u].size();
63     int cnt=0;
64     for(int i=0;i<n0;i++){
65         int v=V[u][i].v;
66         if(d[u]>d[v])
67             cnt+=dfs(v);
68     }
69     return ans[u]=cnt;
70 }
71 int main(void)
72 {
73     int a,b,c;
74     while(scanf("%d",&n),n)
75     {
76         scanf("%d",&m);
77         for(int i=0;i<=n;i++) V[i].clear();
78         memset(ans,0,sizeof(ans));
79         for(int i=0;i<m;i++){
80             scanf("%d%d%d",&a,&b,&c);
81             V[a].push_back(node(b,c));
82             V[b].push_back(node(a,c));
83         }
84         spfa(2);
85         printf("%d\n",dfs(1));
86     }
87     return 0;
88 } 
View Code

dij:

 1 //62MS    4192K    1280 B    C++
 2 /*
 3     dij 解法..
 4     开始竟然把dij写错了,有想撞墙的冲动.. 
 5 */
 6 #include<stdio.h>
 7 #include<string.h>
 8 #define inf 0x7ffffff
 9 int d[1005];
10 int vis[1005];
11 int g[1005][1005];
12 int ans[1005];
13 int n,m,cnt;
14 void dij(int s)
15 {
16     memset(vis,0,sizeof(vis));
17     for(int i=0;i<=n;i++)
18         d[i]=g[s][i];
19     vis[s]=1;
20     d[s]=0;
21     for(int i=1;i<n;i++){
22         int temp=inf;
23         int v=0;
24         for(int j=1;j<=n;j++){
25             if(!vis[j] && temp>d[j]){
26                 v=j;
27                 temp=d[j];
28             }
29         }
30         vis[v]=1;
31         for(int j=1;j<=n;j++)
32             if(!vis[j] && d[j]>d[v]+g[v][j])
33                 d[j]=d[v]+g[v][j];
34     }
35 }
36 int dfs(int u)
37 {
38     if(ans[u]>0) return ans[u];
39     if(u==2) return 1;
40     int cnt=0;
41     for(int i=1;i<=n;i++)
42         if(d[u]>d[i] && g[u][i]<inf){
43             cnt+=dfs(i);
44         }
45     return ans[u]+=cnt;
46 }
47 int main(void)
48 {
49     int a,b,c;
50     while(scanf("%d%d",&n,&m),n)
51     {
52         for(int i=0;i<=n;i++)
53             for(int j=0;j<=n;j++)
54                 g[i][j]=inf;
55         for(int i=0;i<m;i++){
56             scanf("%d%d%d",&a,&b,&c);
57             if(g[a][b]>c) 
58                 g[a][b]=g[b][a]=c;
59         }
60         dij(2);
61         memset(ans,0,sizeof(ans));
62         printf("%d\n",dfs(1));
63     }
64     return 0;
65 } 
View Code

 

posted @ 2013-11-03 09:46  heaventouch  阅读(114)  评论(0编辑  收藏  举报