HDU 1874 smooth Engineering (Dijkstra)

Source: Internet
Author: User
HDU 1874 smooth engineering continued

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

Problem description a province has finally built many roads since its smooth engineering plan was implemented for many years. 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.

The input question 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), indicating that there is a two-way road with 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.

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

Sample input3 3 0 1 1 0 2 3 1 1 1 0 2 1 1 1 2

Sample output2-1

 

AC:

# Include <iostream>
Using namespace STD;
# Define INF 99999
# Deprecision Max 201
Int map [Max] [Max], DIS [Max], V [Max];
Void Dijkstra (int s, int N)
{
Int I, K, J, min;
For (I = 0; I <n; I ++)
Dis [I] = map [s] [I];
Memset (v, 0, sizeof (V ));
V [s] = 1;
Dis [s] = 0;
For (I = 1; I <n; I ++)
{
K = s;
Min = inf;
For (j = 0; j <n; j ++)
If (! V [J] & dis [J] <min)
{
K = J;
Min = dis [J];
}
V [k] = 1;
For (j = 0; j <n; j ++)
If (! V [J] & map [k] [J] <inf)
{
If (DIS [J]> dis [k] + map [k] [J])
Dis [J] = dis [k] + map [k] [J];
}
}
}
Int main ()
{
Int n, m;
While (~ Scanf ("% d", & N, & M ))
{
Int I, j, X, Y, Q, S, T;
For (I = 0; I <n; I ++)
For (j = 0; j <n; j ++)
Map [I] [J] = inf;
For (I = 0; I <m; I ++)
{
Scanf ("% d", & X, & Y, & Q );
If (Map [x] [Y]> q)
Map [x] [Y] = map [y] [x] = Q;
}
Scanf ("% d", & S, & T );
Dijkstra (S, N );
If (DIS [T]! = Inf)
Printf ("% d \ n", DIS [T]);
Else
Printf ("-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.