This question uses the shortest path and DP knowledge. It feels like a good question.
First, based on the diagram provided by the original question, 2 has been used as the start point to find the single-source shortest path, and then, based on what the question says, when d [I]> d [J], it can be from I to J, so another graph can be created according to this requirement, this figure is a typical "number Tower" model, so we can find the number of routes by DP.
For more information, see code implementation.
# Include <iostream> # include <cstdio> # include <vector> using namespace STD; const int maxn = 1005; const int INF = (1 <30 ); vector <int> edge [maxn]; // store the int map [maxn] [maxn] of the walking edge; // The original graph int d [maxn]; // d [I] indicates the distance from I to 2. Int DP [maxn]; // DP [I] indicates the number of paths from I to 2. bool visit [maxn]; void DFS (INT v) {If (DP [v]! =-1) return; DP [v] = 0; For (INT I = 0; I <edge [v]. size (); I ++) {int u = edge [v] [I]; DFS (U); DP [v] + = DP [u] ;}} int main () {int I, j, n, m; int A, B, C; while (scanf ("% d", & N), n) {scanf ("% d", & M); for (I = 1; I <= N; I ++) {visit [I] = false; d [I] = inf; For (j = 1; j <= N; j ++) map [I] [J] = inf;} d [2] = 0; while (M --) {scanf ("% d", & A, & B, & C); If (Map [a] [B]> C) {map [a] [B] = C; Map [B] [a] = C ;}} d [0] = inf; while (1) {J = 0; for (I = 1; I <= N; I ++) if (! Visit [I] & D [I] <D [J]) J = I; If (j = 0) break; visit [J] = true; for (I = 1; I <= N; I ++) if (! Visit [I] & D [J] + map [J] [I] <D [I]) d [I] = d [J] + map [J] [I] ;}for (I = 1; I <= N; I ++) edge [I]. clear (); for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) if (Map [I] [J]! = Inf & D [I]> d [J]) edge [I]. push_back (j); for (I = 1; I <= N; I ++) DP [I] =-1; DP [2] = 1; DFS (1 ); printf ("% d \ n", DP [1]);} return 0 ;}