Analysis: Find Jimmy from the office through the Forest back home (that is, from point 1 to 2) The shortest path there are how many, in which to meet if you want to go to a to B this road, then there is a to the end of the distance from a to the end of the distance is greater than B.
Solution: SPFA algorithm + memory Deep Search
1. SPFA find the shortest path from the end point 2 to all other points
2, the memory of Dfs from 1 start to other points deep search, the final result is dp[1].
#include <iostream> #include <queue> using namespace std; int u[2000002];int v[2000002];int w[2000002];bool vis[1001];int d[1001];int first[1001];int Next[2000002];int dp[1001] ; void Init (int n,int m) {int i; for (i=1;i<=n;i++) {vis[i]=false; First[i]=-1;dp[i]=-1; } for (i=0;i<m;i++) next[i]=-1;} int DFS (int u) {int i,sum;if (DP[U]!=-1) return dp[u]; Memory if (u==2) return 1; End Sum=0;for (I=first[u];i!=-1;i=next[i]) {if (D[u]>d[v[i]) Sum+=dfs (V[i]);} return dp[u]=sum;} void SPFA (int n,int s) {queue<int> q; int i,x,y; for (i=1;i<=n;i++) d[i]= (i==s)? 0:0x7fffffff; Initialize yourself to yourself as 0, other to yourself equivalent to infinity Q.push (s); while (!q.empty ()) {X=q.front (); Q.pop (); Vis[x]=false; for (I=first[x];i!=-1;i=next[i]) {y=v[i]; if (D[y]>d[x]+w[i]) {d[y]=d[x]+w[i]; If(!vis[y]) {vis[y]=true; Q.push (y); }}}}} void Read (int m) {int i,a,b; for (i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&w[i]); U[i]=a; Store forward edge, because it is an v[i]=b graph; Next[i]=first[a]; First[a]=i; W[i+1]=w[i]; Store reverse Edge i++; U[i]=b; V[i]=a; NEXT[I]=FIRST[B]; First[b]=i; }} int main () {int n,m; while (scanf ("%d", &n) ==1 && N) {scanf ("%d", &m); Init (N,M+M); M+m is because there is no edge, each to save two Read (m+m); SPFA (n,2); Find the shortest way from 2 to all other points Cout<<dfs (1) <<endl; Starting from 1 deep search, the final result} return 0; }
HDU ACM 1142 A Walk Through the FOREST->SPFA algorithm + memory Deep Search