HDU 1142 a walk through the forest Dijkstra

Source: Internet
Author: User

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 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.