HDU 2066 a person's trip (Dijkstra)

Source: Internet
Author: User
HDU 2066 travel by one person

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

Problem description although caoer is a luchi (that is, the person who has been in Hangzhou for more than a year will still be lost on campus, Khan ~), However, caoer still enjoys traveling, because he will meet many people (Prince Charming, ^ 0 ^) on the road. Many things can enrich his experience, you can also see beautiful scenery ...... Caoer wants to go to many places. She wants to go to the Tokyo Tower to see the night view, go to Venice to see the movie, go to Yangmingshan to see the taro, go to New York to see the pure snow scene, go to Paris to drink coffee and write, visit Meng jiangnv in Beijing ...... The winter vacation is approaching. You can't waste it for such a long period of time. You must give yourself a good vacation, but you can't waste your training, therefore, caoer decided to go to a desired place in the shortest time! Because caoer's home is in a small town without passing by train, she can only take a train to the neighboring city ~).

There are multiple groups of input data. The first line of each group is three integers t, s, and D, indicating that there are t routes and S are adjacent to cao'er's city, there are d places to go;
Then there are t rows. Each row has three integers A, B, and time, indicating that the driving distance between cities A and B is time hour. (1 = <(A, B) <= 1000; there may be multiple routes between A and B)
The next line t + 1 contains the number of S, indicating the city connected to cao'er's home;
The next line T + 2 has the number D, which indicates that the grass wants to go to the place.

Output outputs the shortest time for a favorite city.

Sample input6 2 3 1 3 5 1 4 7 2 8 12 3 8 4 4 9 12 9 10 2 1 2 8 9 10

Sample output9

 

# Include <iostream>
Using namespace STD;
# Define INF 999999
Int dis [1001], V [1001];
Int map [1001] [1001];
Void Dijkstra (int s, int N)
{
Int I, J, K, min;
Memset (v, 0, sizeof (V ));
For (I = 1; I <= N; I ++)
Dis [I] = map [s] [I];
V [s] = 1;
For (I = 2; I <= N; I ++)
{
K = s;
Min = inf;
For (j = 1; j <= N; j ++)
If (! V [J] & dis [J] <min)
{
K = J;
Min = dis [J];
}
V [k] = 1;
For (j = 1; 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 T, S, D, a [1001], B [1001];
While (~ Scanf ("% d", & T, & S, & D ))
{
Int J, I, AA, BB, P;
For (I = 1; I <= 1000; I ++)
For (j = 1; j <= 1000; j ++)
Map [I] [J] = inf;
While (t --)
{
Scanf ("% d", & aa, & BB, & P );
If (Map [AA] [BB]> P)
Map [AA] [BB] = map [BB] [AA] = P;
}
For (I = 1; I <= s; I ++)
Scanf ("% d", & A [I]);
For (I = 1; I <= D; I ++)
Scanf ("% d", & B [I]);
Int min = inf;
For (I = 1; I <= s; I ++)
{
Dijkstra (A [I], 1000 );
For (j = 1; j <= D; j ++)
If (min> dis [B [J])
Min = dis [B [J];
}
Printf ("% d \ n", min );
}
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.