A Walk Through the Foresttime limit:2000/1000ms (java/other) Memory limit:65536/32768k (Java/other) Total Submission ( s): 8 Accepted Submission (s): 5font:times New Roman | Verdana | Georgiafont Size:←→problem Descriptionjimmy experiences a lot of stress in work these days, especially since his Acciden T made working difficult. 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. Inputoutputfor each test case, output a single integer indicating the number of different routes through the forest. Assume that is does not exceed 2147483647Sample 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
Train of thought: 1428 campus stroll with HDU
First to find out the BFS, each point to the end of the distance (for the latter can walk as the basis for judgment), and then statistics, the number of routes
#include <iostream>#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<algorithm>using namespacestd;Const intinf=0x7fffffff;Const intmaxn=1005;intn,m,i,j;Long LongF[MAXN],DIS[MAXN];intST[MAXN];structnode{intnum,d; Node (intAintb) {num=a; d=b;}}; Vector<node>S[MAXN];BOOLcmpintAintb) { returnDis[a]>dis[b];}voidBFs () {Queue<int>Q; Q.push (2); //memset (vis,0,sizeof (Vis));dis[2]=0; while(!Q.empty ()) { intu=Q.front (); Q.pop (); for(intI=0; I<s[u].size (); i++) { if(Dis[u]+s[u][i].d>=dis[s[u][i].num])Continue; Dis[s[u][i].num]=dis[u]+S[U][I].D; Q.push (S[u][i].num); //You cannot use the vis[] array to determine whether or not you have accessed. //Cause: If you have been visited, not necessarily in the queue, you cannot update other points. } } return;}intMain () { while(SCANF ("%d",&N)) {if(n==0) Break; scanf ("%d",&m); for(i=1; i<=n;i++) {s[i].clear (); Dis[i]=inf; } for(i=1; i<=m;i++) { intx, Y, Z scanf ("%d%d%d",&x,&y,&z); S[x].push_back (Node (y,z)); S[y].push_back (Node (x,z)); } BFS (); for(i=1; i<=n;i++) st[i]=i; Sort (St+1, st+n+1, CMP); Memset (F,0,sizeof(f)); f[1]=1; for(i=1; i<=n;i++) { for(j=0; J<s[st[i]].size (); j + +) if(dis[s[st[i]][j].num]>Dis[st[i]]) F[st[i]+=F[s[st[i]][j].num]; } printf ("%lld\n", f[2]); } return 0;}
HDU 1142 A Walk Through the Forest