A Walk Through the Forest
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 6935 Accepted Submission (s): 2548
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 cont Ains 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 this number does not exceed 2147483647 sample Input5 61 3 21 4 23 4 31 5 124 2 345 2 247 81 3 11 4 7 4 5 7 2 2 10 sample output24 sourceuniversity of Waterloo Local Contest 2005.09.24 test Instructions is the number of path bars that satisfy the condition from start 1 to end 2, provided that all edge AB on the path satisfies the shortest short-circuit with a-to-end point greater than B to the end point. The idea is dijkstra+. Memory Search
/*id:linkarftcprog:1142.cpplang:c++*/#include<map>#include<Set>#include<cmath>#include<stack>#include<queue>#include<vector>#include<cstdio>#include<string>#include<utility>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;#defineEPS 1e-8#defineRandin Srand ((unsigned int) time (NULL))#defineInput freopen ("Input.txt", "R", stdin)#defineDebug (s) cout << "s =" << s << endl;#defineOutstars cout << "*************" << Endl;Const DoublePI = ACOs (-1.0);Const DoubleE = exp (1.0);Const intINF =0x3f3f3f3f;Const intINF =0x7fffffff; typedefLong Longll;Const intMAXN =1010;intN, M;intMP[MAXN][MAXN];intDIS[MAXN];BOOLVIS[MAXN];voidDij (ints) { for(inti =1; I <= N; i + +) dis[i] =Mp[s][i]; memset (Vis,0,sizeof(VIS)); Vis[s]=true; Dis[s]=0; for(inti =2; I <= N; i + +) { intMi =inf; intII = S;//to assign a value because the for loop of the next line may not change the value of II, so the re may for(intj =1; J <= N; J + +) { if(!vis[j] && Dis[j] <mi) {mi=Dis[j]; II=J; }} Vis[ii]=true; for(intj =1; J <= N; J + +) { if(!vis[j] && dis[j] > Dis[ii] +Mp[ii][j]) {Dis[j]= Dis[ii] +Mp[ii][j]; } } }}intCNT[MAXN];intDfsintcur) { if(Cnt[cur])returnCnt[cur]; if(cur = =2)return 1; intRET =0; for(inti =1; I <= N; i + +) { if(Mp[cur][i] = = INF | | dis[i] >= dis[cur])Continue; RET+=DFS (i); } Cnt[cur]=ret; returnret;}intMain () {//input; intu, V, c; while(~SCANF ("%d", &n) &&N) {scanf ("%d", &m); Memset (MP,0x3f,sizeof(MP)); for(inti =1; I <= m; i + +) {scanf (" %d%d%d", &u, &v, &c); MP[U][V]=C; Mp[v][u]=C; } dij (2); //for (int i = 1; I <= n; i + +) printf ("dis[%d] =%d\n", I, Dis[i]);memset (CNT,0,sizeof(CNT)); printf ("%d\n", DFS (1)); } return 0;}
HDU1142 A Walk Through the Forest