A Walk Through the Forest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7601 Accepted Submission(s): 2796
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
#include <iostream> #include <cstring> #include<stdio.h> //#define INT 1000000000+5 如果 2*INT 2*1000000000+5则值是2000000005 using namespace std; const int INT=0x3f3f3f3f;//定义常量,最好用const ,本题必须用16进制 用10进制表示INT 会出错 我也不知道为什么 const int maxn=1005; int dist[maxn]; int map[maxn][maxn]; bool vis[maxn]; int dp[maxn]; int dian,side; void Dijkstra(int x) { for(int i=1;i<=dian;i++) { dist[i]=map[x][i];//点2到各个点的距离记录到dist[i]中 } memset(vis,false,sizeof(vis));//标记所有是没走过 vis[x]=true; dist[x]=0;//一定要确定这一步 int index; for(int i=1;i<=dian;i++) { int minn=INT; for(int ii=1;ii<=dian;ii++)//遍历所有点,找出第一个到2的最小距路径 标记给index { if(!vis[ii]&&dist[ii]<minn)//也就是所有2能到达的点中,index到2的距离最短 { minn=dist[ii]; index=ii; } } if(minn==INT) break;//如果所有的点都无法到达 2 退出循环 vis[index]=true; for(int j=1;j<=dian;j++)//更新一遍所有的点到2的距离 { if(!vis[j]&&dist[index]+map[index][j]<dist[j])//没标记过 && 符合题意的条件 { dist[j]=dist[index]+map[index][j]; } } } } int DFS(int x) { int sum=0; if(dp[x]) return dp[x]; for(int i=1;i<=dian;i++) { if(map[x][i]!=INT&&dist[x]>dist[i])//(每次搜索一条能走的路&&下一个点i到2的距离要小于这个点到2的距离) { //printf("i=%d \n",i); sum+=DFS(i); //printf("%d\n",i); } } return dp[x]=sum; } int main (void) { int a,b; int far; while(~scanf("%d",&dian),dian) { scanf("%d",&side); memset(map,INT,sizeof(map)); for(int i=1; i<=side; i++) { scanf("%d%d%d",&a,&b,&far); map[a][b]=map[b][a]=far; } Dijkstra(2); /* for(int i=1;i<=dian;i++) { printf("i=%d dist=%d\n",i,dist[i]); }*/ memset(dp,0,sizeof(dp)); dp[2]=1; printf("%d\n",DFS(1)); } return 0; }