A Walk Through the ForestTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 6277 Accepted Submission (s): 2313
Problem Descriptionjimmy experiences a lot of stress at work these days, especially since he accident made working diffic Ult. To relax after a hard day, he likes to walk home. To make things even nicer, he office is on one side of a forest, and he house was on the other. A Nice walk through the forest, seeing the birds and chipmunks are 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 is always takes a path to make progress towards his house. He considers taking a path from a to B to being progress if there exists a route from B to his home that's shorter than any Possible route from A. Calculate how many different routes through the forest Jimmy might take.
Inputinput contains several test cases followed by a line containing 0. Jimmy had numbered each intersection or joining of paths starting with 1. His office is numbered 1, and he house is numbered 2. The first line of all 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 o F Length D between intersection A and a different intersection B. Jimmy may walk a path any direction he chooses. There is at the most one of the path between any pair of intersections.
Outputfor each test case, output a single integer indicating the number of different routes through the forest. Assume that is does not exceed 2147483647
Sample Input
5 61 3 21 4 23 4 31 5 124 2 345 2 247 81 3 11 4 13 7 17 4 17 5 16 7 15 2 16 2 10
Sample Output
24
#include <stdio.h> #include <string.h>const int maxint = 0xfffffff;const int max= 1010;int dis[max],map[max][ Max];int Path[max],ans[max]; BOOL Visit[max];int n,m;void Dijkstra (int s) {int i,j,k,p=0,min;for (i=1;i<=n;++i) {dis[i]=map[s][i];p ath[i]=i;} static bool Visit[max];memset (Visit,0,sizeof (visit));d is[s]=0;path[s]=0;visit[s]=1;for (k=1;k<n;++k) {for (min =maxint,i=1;i<=n;++i) {if (!visit[i]&&dis[i]<min) p=i,min=dis[i];} if (min>=maxint) break;for (visit[p]=1,i=1;i<=n;++i) {if (!visit[i]&&map[p][i]<maxint) {if (min+map[ P][i]<dis[i]) dis[i]=min+map[p][i],path[i]=p;}} /*if (Visit[t]) {int min_dis=dis[t];return min_dis;} */}}int dfs (int v) {if (ans[v]!=-1) return ans[v];if (v==2) return 1; ans[v]=0;for (int i=1;i<=n;++i) {if (Map[v][i]!=maxint&&dis[i]<dis[v]) {Ans[v]+=dfs (i);}} return ans[v];} int main () {//int n,m;while (~scanf ("%d", &n), N) {scanf ("%d", &m), int i,j,a,b,d;for (i=1;i<=n;++i) {for (j=1;j <=N;++J) Map[i][j]=maxint;} while (m--{scanf ("%d%d%d", &a,&b,&d), if (map[a][b]>d) Map[a][b]=map[b][a]=d;} Dijkstra (2); Memset (ans,-1,sizeof (ans));p rintf ("%d\n", DFS (1));} return 0;}
Hdoj-1142-a Walk Through the Forest