HDU 1142 a walk through the forest (Dijkstra)

Source: Internet
Author: User
HDU 1142 a walk through the forest

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1142

Problem descriptionjimmy experiences a lot of stress at work these days, especially since his accident made working difficult. to relax after a hard day, he likes to walk home. to make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable.
The forest is beautiful, and Jimmy wants to take a different route everyday. he also wants to get home before dark, so he always takes a path to make progress towards his house. he considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from. calculate how many different routes through the forest Jimmy might take.

Inputinput contains several test cases followed by a line containing 0. jimmy has numbered each intersection or joining of paths starting with 1. his office is numbered 1, and his house is numbered 2. the first line of each test case gives the number of intersections N, 1 <n ≤ 1000, and the number of paths M. the following M lines each contain a pair of intersections a B and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length D between Intersection a and a different intersection B. jimmy may walk a path any direction he chooses. there is at most one path between any pair of intersections.

Outputfor each test case, output a single integer indicating the number of different routes through the forest. You may assume that this number does not exceed 2147483647

Sample input5 6 1 3 1 4 2 3 3 3 1 5 12 4 2 34 5 2 24 7 8 1 1 1 1 4 1 3 7 1 7 4 1 1 7 1 7 5 1 6 7 1 5 2 1 6 2 1 0

Sample output2 4

 

1.1 is the start point, 2 is the end point, because to take the AB path, you must ensure that the condition, so from the end point to use the single-source shortest path DijkstraAlgorithmThe shortest path is obtained as the minimum path for finding the path.

2. Search for each path to see how many paths meet the question. Of course, this needs to be searched from the start point, because the DIS [I] array stores the shortest distance from this point to the end point.

3. After this search, DP [1] is the number of all paths from the start point to the end point that meet the meaning of the question.

 

# Include <iostream>
Using namespace STD;
# Define INF 9999999
# Deprecision Max 1001
Int map [Max] [Max];
Int dis [Max], V [Max], sum [Max];
Void Dijkstra (int s, int N)
{
Int I, j, Min, K;
For (I = 1; I <= N; I ++)
Dis [I] = map [s] [I];
Memset (v, 0, sizeof (V ));
V [s] = 1;
Dis [s] = 0;
For (I = 2; I <= N; I ++)
{
K = s;
Min = inf;
For (j = 1; j <= N; j ++)
If (! V [J] & min> dis [J])
{
Min = dis [J];
K = J;
}
V [k] = 1;
For (j = 1; j <= N; j ++)
If (! V [J] & dis [J]> dis [k] + map [k] [J])
Dis [J] = dis [k] + map [k] [J];
}
}
Int DFS (int s, int N)
{
Int I;
If (sum [s])
Return sum [s];
If (S = 2)
Return 1;
For (I = 1; I <= N; I ++)
If (Map [s] [I]! = Inf)
{
If (DIS [s]> dis [I])
Sum [s] + = DFS (I, n );
}
Return sum [s];
}
Int main ()
{
Int n, m;
While (~ Scanf ("% d", & N), n)
{
Scanf ("% d", & M );
Int A, B, Q;
Int I, J;
For (I = 1; I <= N; I ++)
For (j = 1; j <= N; j ++)
Map [I] [J] = inf;
While (M --)
{
Scanf ("% d", & A, & B, & Q );
If (Map [a] [B] = inf | map [a] [B]> q)
Map [a] [B] = map [B] [a] = Q;
}
Dijkstra (2, N );
Memset (sum, 0, sizeof (SUM ));
Printf ("% d \ n", DFS (1, N ));
}
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.