HDU 1874 smooth engineering continued

Source: Internet
Author: User

 
Display result:

 


Smooth engineering resumption
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 19989 Accepted Submission (s): 6912

 


Problem Description
After many years of smooth engineering planning, a province has finally built many roads. However, when there are too many roads, there are many ways to choose from every town to another town, some solutions are much shorter than others. This makes pedestrians very difficult.

Now you know the start and end points. Calculate the shortest distance from the start point to the end point.


Input
This topic contains multiple groups of data. Please process the data until the end of the file.
The first row of each data group contains two positive integers N and M (0 <N <1000, 0 <M <), representing the number of existing towns and the number of constructed roads respectively. The towns are 0 ~ N-1 number.
Next is M-line road information. Each row has three integers, A, B, X (0 <= A, B <N,! = B, 0 <X <10000) indicates that there is an X length between town A and town B.
The next line has two integers, T (0 <= S, T <N), representing the start point and the end point respectively.


Output
For each group of data, output the shortest distance in one row. If there is no route from S to T, output-1.

 

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

Sample Output
2
-1

Idea: Use the Dijistra algorithm to find the shortest path

Conclusion: The Dijistra algorithm is used to obtain the shortest path. We need to calculate the distance from the start point to all vertices and the shortest path from them. What I started to calculate is, the distance from the start to the end: the result is WA many times before it is found.

Import java. io. *; import java. util. *; public class Main {public static int M = 202; public static int MAX = 2000000; public static int map [] [] = new int [M] [M]; public static ArrayList <Integer> ay; public static int n, m, s, t; public static void main (String [] args) {consumer SC = new consumer (new BufferedInputStream (System. in); while (SC. hasNextInt () {ay = new ArrayList <Integer> (); // stores the distance from the start point to the city. // initializes the map for (int I = 0; I <M; I ++) {for (int j = 0; j <M; j ++) {map [I] [j] = MAX ;}} n = SC. nextInt (); m = SC. nextInt (); for (int I = 0; I <m; I ++) {int a = SC. nextInt (); int B = SC. nextInt (); int x = SC. nextInt (); // the same path between the two towns. if (map [a] [B]> x) {map [a] [B] = map [B] [a] = x; // a two-way road between two towns} s = SC. nextInt (); t = SC. nextInt (); getDistance (s); if (ay. get (t) <MAX) // if the value is smaller than the maximum value, the route exists. Otherwise, the System does not exist. out. println (ay. get (t); else System. out. println ("-1") ;}} public static void GetDistance (int v) {boolean boo [] = new boolean [M]; // determines whether the town has passed through int k = 0; for (int I = 0; I <= n; I ++) {ay. add (map [v] [I]); // The distance from the start point to the towns} boo [v] = true; ay. set (v, 0); for (int I = 0; I <= n; I ++) {int min = MAX; // determine the distance between towns, the smallest distance. For (int j = 0; j <= n; j ++) {if (! Boo [j] & ay. get (j) <min) {min = ay. get (j); k = j ;}} boo [k] = true; if (min = MAX) break; // get to a new town, calculate the distance from it to each city. for (int j = 0; j <= n; j ++) {if (! Boo [j] & ay. get (j)> ay. get (k) + map [k] [j]) {ay. set (j, ay. get (k) + map [k] [j]) ;}}}}

 

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.