Hdoj 1874 HDU 1874 smooth engineering ACM 1874 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, 1874
Description: Smooth engineering resumption
Time limit: 3000 / 1000 MS (Java / Others) memory limit: 32768 / 32768 K (Java / Others)
Total submission (s ): 5528 Accepted submission (s ): 1686

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 < 200 , 0 < M < 1000 Represents the number of existing towns and the number of roads that have been built. The towns are 0 ~ N - 1.
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 a two-way road with x length between town a and town B.
The next line has two integers, S ( 0 <= S, T < N), indicating 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

Question Analysis: The most short-circuit entry question.

DijkstraAlgorithmThe basic idea 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.

Code As follows: # Include < Iostream >
Using   Namespace STD;
Const   Int Max =   201 ;
Const   Int INF =   0x7ffffff ;
Int Graph [Max] [Max];
Bool Hash [Max];
Int Path [Max];
Int N, m;
Int Dijkstra ( Int Beg, Int End)
{
Path [beg] =   0 ;
Hash [beg] =   False ;
While (Beg ! = End)
{
Int M = INF, temp;
For ( Int I =   0 ; I ! = N; ++ I)
{
If (Graph [beg] [I] ! = INF)
Path [I] = Min (path [I], path [beg] + Graph [beg] [I]);
If (M > Path [I] && Hash [I])
{
M = Path [I];
Temp = I;
}
}
Beg = Temp;
If (M = INF)
Break ;
Hash [beg] =   False ;
}
If (Path [end] = INF)
Return   - 1 ;
Return Path [end];
}
Int Main ()
{
While (Scanf ( " % D " , & N, & M) ! = EOF)
{
For ( Int I =   0 ; I ! = Max; ++ I)
{
Hash [I] =   True ;
Path [I] = INF;
For ( Int J =   0 ; J ! = Max; ++ J)
{
Graph [I] [J] = INF;
}
}
For ( Int I =   0 ; I ! = M; ++ I)
{
Int C1, C2, cost;
Scanf ( " % D " , & C1, & C2, & Cost );
If (Cost < Graph [C1] [C2])
Graph [C1] [C2] = Graph [C2] [C1] = Cost;
}
Int Beg, end;
Scanf ( " % D " , & Beg, & End );
Cout < Dijkstra (beg, end) < 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.