HDOJ 2544 HDU 2544 Shortest Path ACM 2544 IN HDU

Source: Internet
Author: User
MiYu original, post Please note: Reprinted from __________ White House

Question address:
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 2544
Description: The shortest path.
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 3844 Accepted Submission (s): 1628

Problem Description
In each year's competition, all the finalists will get a very beautiful t-shirt. However, every time our staff moved hundreds of pieces of clothing from the store back to the stadium, they were very tired! So now they want to find the shortest route from the store to the stadium. Can you help them?

 

Input
The input includes multiple groups of data. The first row of each group of data is two integers, N and M (N <= 100, M <= 10000). N indicates several intersections on the streets of Chengdu, the intersection marked as 1 is the location of the store, the intersection marked as N is the location of the stadium, and M represents several roads in Chengdu. N = M = 0 indicates that the input is complete. In the next M row, each row contains three integers, A, B, and C (1 <= A, B <= N, 1 <= C <= 1000 ), it means there is A road between Intersection A and intersection B. Our staff need to walk this road in C minutes.
Enter a route to ensure there is at least one store.

 

Output
Output a line for each group of inputs, indicating the shortest time for a staff member to walk from the store to the stadium
 

Sample Input
2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0
 

Sample Output
3
2

Question Analysis: I was just in touch with the most short-circuit entry question. I didn't understand what was going on at the beginning. I woke up with a sleep, I flipped out the data structure book and reviewed it once. Dijkstra finally understood it.

The basic idea of Dijkstra algorithm is:
Assume that each vertex has a pair of labels (dj, pj ), where, the dj is the length of the shortest path from the origin point s to the j (the shortest path from the vertex to its own is zero (no arc path), and its length is equal to zero );

Pj is the first point of the j point in the shortest path from s to j. The basic process for solving the shortest path algorithm from the origin point s to j is as follows:

1) initialization. Origin point: ① ds = 0, ps is empty; ② All other points: di = ∞, pi = ?; (3) mark the origin point s, and remember k = s. All other points are set to unmarked.

2) Check the distance from all marked vertices k to their directly connected unlabeled vertices j, and set:

Dj = min [dj, dk + lkj]

In formula, lkj is the direct connection distance from vertex k to j.

3) Select the next vertex. Select the smallest I in the dj from all unmarked nodes:

Di = min [dj, all unlabeled vertices j]

Point I is selected as a point in the shortest path and set as marked.

4) Find the first point of vertex I. Find the vertex j * that is directly connected to vertex I from the marked vertex. As the previous vertex, set: I = j *

5) mark point I. If all vertices have been marked, the algorithm is completely introduced. Otherwise, remember k = I, go to 2) and continue.

The Code is as follows: # include <iostream>
Using namespace std;
Const int INF = 0x7FFFFFFF;
Const int MAX = 105;
Int graph [MAX] [MAX];
Int N, M;
Int Dijkstra (int beg, int end)
{
Bool hash [N + 1];
Int path [N + 1];
For (int I = 0; I <= N; ++ I)
{
Hash [I] = true;
Path [I] = INF;
}
Hash [beg] = false;
Path [beg] = 0;
While (beg! = End)
{
For (int I = 1; I <= N; ++ I)
{
If (graph [beg] [I])
{
If (path [I]> path [beg] + graph [beg] [I])
Path [I] = path [beg] + graph [beg] [I];
}
}
Int min = INF;
For (int I = 1; I <= N; ++ I)
{
If (min> path [I] & hash [I])
{
Min = path [I];
Beg = I;
}
}
Hash [beg] = false;
}
Return path [end];
}

Int main ()
{
While (scanf ("% d", & N, & M), N + M)
{
Memset (graph, 0, sizeof (graph ));
For (int I = 1; I <= M; ++ I)
{
Int r, c, cost;
Scanf ("% d", & r, & c, & cost );
If (graph [r] [c] = 0)
Graph [r] [c] = graph [c] [r] = cost;
Else
{
If (cost <graph [r] [c])
Graph [r] [c] = graph [c] [r] = cost;
}
}
Cout <Dijkstra (1, N) <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.