HDU 2544 Shortest Path

Source: Internet
Author: User

Shortest Path
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 22174 Accepted Submission (s): 9436

 


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

Idea: Use the Dijistra algorithm to find the shortest path

 

 

Import java. util. *; import java. io. *; public class Main {public static int M = 102; public static int MAX = 2000000; public static int map [] [] = new int [M] [M]; public static consumer list <Integer> list; public static int n, m; public static void main (String [] args) {consumer SC = new consumer (new BufferedInputStream (System. in); while (SC. hasNextInt () {// initialize the map for (int I = 0; I <M; I ++) {for (int j = 0; j <M; j ++) {map [I] [j] = MAX ;}} li St = new intersection list <Integer> (); // The distance from the store location to the intersection n = SC. nextInt (); m = SC. nextInt (); if (n = 0 & m = 0) System. exit (0); // input n = 0 and m = 0 end program for (int I = 0; I <m; I ++) {int a = SC. nextInt (); int B = SC. nextInt (); int time = SC. nextInt (); if (map [a] [B]> time) map [a] [B] = map [B] [a] = time; // The time for returning the same route is the same} getDistance (1); System. out. println (list. get (n) ;}}// Dijistra algorithm public static void getDistance (int v) {int k = 0; boolean [] boo = new boole An [M]; // indicates whether the intersection has passed for (int I = 0; I <= n; I ++) {list. add (map [v] [I]);} boo [v] = true; // true indicates the list of paths that have elapsed. set (v, 0); // judge each intersection for (int I = 0; I <= n; I ++) {int min = MAX; // find the intersection and use the shortest time for (int j = 0; j <= n; j ++) {if (! Boo [j] & list. get (j) <min) {min = list. get (j); k = j ;}} boo [k] = true; if (min = MAX) break; // to a new intersection, re-determine the time it took to reach each intersection for (int j = 0; j <= n; j ++) {if (! Boo [j] & list. get (j)> list. get (k) + map [k] [j]) {list. set (j, list. get (k) + map [k] [j]) ;}}}}

 

Related Article

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.