HDU 2066 travel by one person (Shortest Path)

Source: Internet
Author: User
Travel by one person Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 18343 accepted submission (s): 6372



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 Input
6 2 31 3 51 4 72 8 123 8 44 9 129 10 21 28 9 10
 
Sample output
9
 
Authorgrass
Sourcerpg exercise session
Recommendlcy | we have carefully selected several similar problems for you: 1874 2112 1217 1142
Code: 78 Ms
# Include <stdio. h> # include <string. h> # define INF 1000000 # define maxn 1100IntWant[Maxn]; IntMap[Maxn] [Maxn]; IntNear[Maxn]; IntMark[Maxn]; IntDIS[Maxn]; IntMC; VoidDijkstra(IntX) {IntI,J,K,Minx;Memset(Mark,0, Sizeof (Mark));// Initialize all nodes, and each node is not selected.For (I=0;I<=MC;I++)DIS[I] =Map[X] [I];// Set the distance from X to I to dis [I]. Mark[X] =1;// Mark X as the accessed dis[X] =0;// Record the distance between each node and x node as the current distance.For (I=1;I<=MC;I++ ){Minx=INF; (J=0;J<=MC;J++) {If (Mark[J] =0&&DIS[J] <Minx)// Select the shortest distance from the unaccessed node.{Minx=DIS[J];K=J;}}Mark[K] =1; (J=0;J<=MC;J++) {If (Mark[J] =0&&DIS[J]>DIS[K] +Map[K] [J])// Update the Shortest Path of the remaining nodes after marking.{DIS[J] =DIS[K] +Map[K] [J] ;}}} Int main () {intMin,I,J,A,B,C,T,S,D; While (Scanf("% D",&T,&S,&D)! =EOF){Min=INF;MC=0; (I=0;I<Maxn;I++) {(J=0;J<Maxn;J++) {If (I=J)Map[I] [J] =0; ElseMap[I] [J] =Map[J] [I] =INF;// The default initial distance is infinite.} (I=1;I<=T;I++ ){Scanf("% D",&A,&B,&C); If (Map[A] [B]>C){Map[A] [B] =C;Map[B] [A] =C;}MC= (A>B?A:B)>MC? (A>B?A:B):MC;// Locate the city with the largest serial number, that is, the destination.} (I=1;I<=S;I++ ){Scanf("% D",&Near[I]);} (I=1;I<=S;I++ ){Map[0] [Near[I] =Map[Near[I] [0] =0;} (I=1;I<=D;I++ ){Scanf("% D",&Want[I]);}Dijkstra(0); (I=1;I<=D;I++) {If (DIS[Want[I] <Min){Min=DIS[Want[I] ;}}Printf("% D \ n",Min);} Return0;}

HDU 2066 travel by one person (Shortest Path)

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.