Question http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1142
I started with this question, but I didn't think it was just a short circuit. I didn't expect a deep search.
First, let's introduce the meaning of this question. I understand that the starting point is "1" and the ending point is "2" on the road between "A and" 2, you can calculate several roads between B and 2 (the distance is smaller than the road between A and 2). A and B are linked ..
Therefore, we need to determine the distance from all points to 2, because 2 is the end point. So we first use Dijkstra to find the distance. In the deep search ....
# Include <iostream> # include <cstring> using namespace STD; int A [1002] [1002], DP [1002], DIS [1002]; bool visit [1002]; int n, m; # define INF 9999999int DFS (INT v) {int I, temp, sum = 0; If (DP [v]! =-1) return DP [v]; If (V = 1) return 1; for (I = 0; I <n; I ++) {if (a [v] [I]! = Inf & dis [v]> dis [I]) // calculate the total number of paths {temp = DFS (I); sum + = temp ;}} DP [v] = sum; return sum;} void Dijkstra (INT v) {int min, K; memset (visit, false, sizeof (visit )); /// color for (INT I = 0; I <n; I ++) dis [I] = A [v] [I]; DIS [v] = 0; for (INT I = 0; I <n; I ++) {min = inf; For (Int J = 0; j <n; j ++) {If (! Visit [J] & dis [J] <min) // find the shortest edge {min = dis [J]; k = J ;}} visit [k] = true; For (Int J = 0; j <n; j ++) {If (! Visit [J] & dis [k] + A [k] [J] <dis [J]) /// no edge is updated. {dis [J] = dis [k] + A [k] [J] ;}}} int main () {int I, j, k; while (CIN> N, N) {for (I = 0; I <n; I ++) {DP [I] =-1; for (j = 0; j <n; j ++) A [I] [J] = inf;} CIN> m; while (M --) {CIN> I> j> K; I = I-1, j = J-1; /// note that I am from 0 if (a [I] [J]> K) A [I] [J] = A [J] [I] = K;} Dijkstra (1); DFS (0 ); cout <DP [0] <Endl;} return 0 ;}