A Walk Through the Forest
Time limit:1000ms Memory limit:65536k
Total submit:48 accepted:15
Description
Jimmy experiences a lot of stress in work these days especially since Hisaccident made working difficult. To relax after a, the he likes to Walkhome. To make things even nicer, he office is on one side of a forest, and hishouse are on the other. A Nice walk through the forest, seeing the birds andchipmunks is quite enjoyable.
The forest is beautiful, and Jimmy wants to take a different route everyday. Healso wants to get home before dark, so he is always takes a path to make progresstowards his house. He considers taking a path from a to B to being progress Ifthere exists a route from B to his home that's shorter than any P Ossible routefrom A. Calculate how many different routes through the forest Jimmy Mighttake.
Input
Input contains several test cases followed by a line containing 0. Jimmyhas numbered each intersection or joining of paths starting with 1. His Officeis numbered 1, and he house is numbered 2. The first line of each test casegives the number of intersections N, 1 < n≤1000, and the number of PATHSM. The following M lines each contain a pair of intersections a B and Aninteger distance 1≤d≤1000000 indicating a path of Length d betweenintersection A and a different intersection B. Jimmy may walk a path anydirection he chooses. There is at the most one of the path between any pair ofintersections.
Output
For each test case, output a single integer indicating the number ofdifferent routes through the forest. Assume that's this number does notexceed 2147483647.
Sampleinput
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
Sampleoutput
2
4
Ideas
Shortest circuit + memory search.
SPFA preprocessing the shortest distance d from each point to the home and then remembers the number of search paths along the D-smaller edge.
Code
1#include <cstdio>2#include <queue>3#include <vector>4#include <cstring>5 using namespacestd;6 7 Const intMAXN = ++Ten;8 Const intinf=1<< -;9 structedge{Ten intU,v,w,next; One}e[2*maxn*MAXN]; A intEN,FRONT[MAXN]; - - intn,m; the -InlinevoidAddedge (intUintVintW) { -en++; E[en].v=v; E[en].w=w; E[en].next=front[u]; front[u]=en; - } + - intD[MAXN]; + voidSPFA (ints) { A intINQ[MAXN]; atqueue<int>Q; -memset (INQ,0,sizeof(INQ)); - for(intI=1; i<=n;i++) d[i]=INF; - -d[s]=0; inq[s]=1; Q.push (s); - while(!Q.empty ()) { in intU=q.front (); Q.pop (); inq[u]=0; - for(inti=front[u];i>=0; i=E[i].next) { to intv=e[i].v,w=E[I].W; + if(d[v]>d[u]+W) { -d[v]=d[u]+W; the if(!Inq[v]) { *inq[v]=1; $ Q.push (v);Panax Notoginseng } - } the } + } A } the + intF[MAXN]; - intdpintu) { $ int& ans=F[u]; $ if(ans>=0)returnans; - - if(u==2)returnans=1; theans=0; - for(inti=front[u];i>=0; i=E[i].next) {Wuyi intv=e[i].v; the if(D[v]<d[u]) ans + = DP (v);//just walk along the smaller d. - } Wu returnans; - } About intMain () { $ while(SCANF ("%d%d", &n,&m) = =2) { -en=-1; -memset (front,-1,sizeof(front)); - intu,v,w; A for(intI=0; i<m;i++) { +scanf"%d%d%d",&u,&v,&W); the Addedge (u,v,w); - Addedge (v,u,w); $ } theSPFA (2); thememset (f,-1,sizeof(f)); theprintf"%d\n", DP (1)); the } - return 0; in}
UVa 10917 A Walk Through the Forest