A Walk Through the Forest
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 1
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.
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
University of Waterloo Local Contest 2005.09.24
题意:Jimmy工作压力大,为了放松,他喜欢走路回家。而为了不太晚到家,他又得朝着家的方向走,也就是说必须离家越来越近。给定一个无向图,求有多少条路径回家。
分析:先用迪杰特斯拉算法求出各结点离源点(家)的最短距离,然后dfs求出路径总数。
时间超限代码(dfs需优化)
1 #include <cstdio> 2 #include <queue> 3 #include <algorithm> 4 #define INF 0x3f3f3f3f 5 using namespace std; 6 7 const int maxn=1001; 8 vector<int> g[maxn]; 9 struct node 10 { 11 int num,dis; 12 }; 13 int n,m; 14 int cost[maxn][maxn],d[maxn]; 15 int ans; 16 17 bool operator<(const node& n1,const node& n2) 18 { 19 return n1.dis<n2.dis; 20 } 21 22 void dij() 23 { 24 fill(d+1,d+n+1,INF); 25 d[2]=0; 26 priority_queue<node> que; 27 que.push(node{2,0}); 28 while(!que.empty()) 29 { 30 int x=que.top().num; 31 que.pop(); 32 for(int i=0;i<g[x].size();i++) 33 { 34 int u=x; 35 int v=g[x][i]; 36 if(d[v]>d[u]+cost[u][v]) 37 { 38 d[v]=d[u]+cost[u][v]; 39 que.push(node{v,d[v]}); 40 } 41 } 42 } 43 } 44 45 void dfs(int x) 46 { 47 if(x==2) 48 { 49 ans++; 50 return ; 51 } 52 for(int i=0;i<g[x].size();i++) 53 { 54 int fx=g[x][i]; 55 if(d[fx]<d[x]) 56 dfs(fx); 57 } 58 } 59 60 int main() 61 { 62 while(scanf("%d",&n),n) 63 { 64 scanf("%d",&m); 65 ans=0; 66 for(int i=1;i<=n;i++) 67 for(int j=1;j<=n;j++) 68 { 69 cost[i][j]=(i==j)?0:INF; 70 } 71 for(int i=0;i<m;i++) 72 { 73 int a,b,c; 74 scanf("%d%d%d",&a,&b,&c); 75 g[a].push_back(b); 76 g[b].push_back(a); 77 cost[a][b]=cost[b][a]=c; 78 } 79 dij(); 80 d[2]=0; 81 dfs(1); 82 printf("%d ",ans); 83 for(int i=1;i<=n;i++) 84 g[i].clear(); 85 ans=0; 86 } 87 return 0; 88 }
优化分析:存在对某些结点进行了多次dfs,导致时间耗费过多。考虑采用记忆化搜索。我们用sum[i]表示从i出发有多少条到家的路径,对于已知sum[i]的结点i就不需要再次dfs,这样dfs的次数就只有n-1次(n为结点数)。
优化后代码:用时514ms
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <algorithm> 5 #define INF 0x3f3f3f3f 6 using namespace std; 7 8 const int maxn=1001; 9 vector<int> g[maxn];//图的邻接表表示 10 struct node 11 { 12 int num,dis; 13 }; 14 int n,m; 15 int cost[maxn][maxn],d[maxn]; 16 int sum[maxn]; 17 18 bool operator<(const node& n1,const node& n2)//重载运算符 19 { 20 return n1.dis<n2.dis; 21 } 22 23 void dij() 24 { 25 fill(d+1,d+n+1,INF);//下标从1开始 26 d[2]=0; 27 priority_queue<node> que; 28 que.push(node{2,0}); 29 while(!que.empty()) 30 { 31 int x=que.top().num; 32 que.pop(); 33 for(int i=0;i<g[x].size();i++) 34 { 35 int u=x; 36 int v=g[x][i]; 37 if(d[v]>d[u]+cost[u][v]) 38 { 39 d[v]=d[u]+cost[u][v]; 40 que.push(node{v,d[v]}); 41 } 42 } 43 } 44 } 45 46 void dfs(int x) 47 { 48 for(int i=0;i<g[x].size();i++) 49 { 50 int fx=g[x][i]; 51 if(d[fx]<d[x]) 52 { 53 if(sum[fx]>0) 54 { 55 sum[x]+=sum[fx]; 56 } 57 else 58 { 59 60 61 dfs(fx); 62 sum[x]+=sum[fx]; 63 } 64 } 65 } 66 // return ; 67 } 68 69 int main() 70 { 71 while(scanf("%d",&n),n) 72 { 73 scanf("%d",&m); 74 memset(sum,0,sizeof(sum)); 75 for(int i=1;i<=n;i++) 76 for(int j=1;j<=n;j++) 77 { 78 cost[i][j]=(i==j)?0:INF; 79 } 80 for(int i=0;i<m;i++) 81 { 82 int a,b,c; 83 scanf("%d%d%d",&a,&b,&c); 84 g[a].push_back(b); 85 g[b].push_back(a); 86 cost[a][b]=cost[b][a]=c; 87 } 88 dij(); 89 sum[2]=1; 90 dfs(1); 91 printf("%d ",sum[1]); 92 for(int i=1;i<=n;i++) 93 g[i].clear(); 94 } 95 return 0; 96 }
此题bug过的地方
(1)fill(d+1,d+1+n,INF),一开始没注意下标是从1开始,写成了fill(d,d+n,INF)。dij函数bug。
(2)每组用例输出后没有清空图,导致下一组用例的图多了许多不属于自己的边和顶点。