HDU Travel of 20,661 persons (Dijkstra)

Source: Internet
Author: User

The journey of a manTime limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 23391 Accepted Submission (s): 8140


Problem description Although grass son is a road crazy (is in Hangzhou electric stay for more than a year, incredibly still will be lost in campus, sweat ~), but grass son still very much like travel, because in the journey will meet a lot of people (Prince Charming, ^0^), many things, but also enrich their experience, You can also see the beautiful scenery ... Grass wants to go to many places, she wants to go to the Tokyo Tower to see the night scene, to Venice to see movies, to see the Taro on Yangmingshan, go to New York pure snow, go to Paris to drink coffee letter, to Beijing to visit Meng Jiangnu ... See the winter vacation is coming, so a large period of time, can not be wasted ah, must give yourself good put a fake, but also can not waste training ah, so grass son decided in the shortest time to a place where they want to go! Because the house of grass is in a small town, there is no train passing by, so she can only go to the neighboring city by train (poor Ah ~).
Input data has more than one group, the first line of each group is three integers t,s and D, indicating that there is a T road, and the grass home adjacent to the city of S, the grass wants to go where there are D;
Then there is a T-line, each line has three integer a,b,time, indicating that the drive between A and B cities is time hours; (1=< (A, B) there may be multiple paths between <=1000;a,b)
The next line of T+1 is the number of S, which indicates the city connected with the grass family;
The next line of T+2 has a number D, indicating that the grass wants to go to the place.
Output outputs the shortest time a grass can go to 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 Practice Competition
Recommendlcy | We have carefully selected several similar problems for you:1874 2112 1217 1548 1142

Shortest path problem, no negative value, Dijkstra algorithm, priority queue implementation

find the shortest path stepThe algorithm steps are as follows: G={v,e} 1. The distance value corresponding to the vertices in the remaining vertex},t in the initial seasonal s={v0},t=v-s={ If there is <v0,vi>,d (V0,VI) as the weight on the <V0,Vi> arc if not present <v0,vi>,d (V0,VI) is ∞ 2. Select a vertex w with the least weight associated with the vertex in S from T and add to S 3. Modify the distance value of the vertices in the remaining T: If you add a W as an intermediate vertex and the distance value from V0 to VI is shortened, the distance value is modified Repeat steps 2 and 3 above until all vertices are included in s, i.e. W=vi

Dijkstra thought is in this picture



Attached code:

#include <stdio.h> #include <string.h> #include <queue> #define INF 0x3fffffffusing namespace Std;int Map[1005][1005],vis[1005],min_road,max_road;struct node{int time,pos;friend bool operator< (node A,node b) {return a.time>b.time;}}; Priority_queue<node>s;int Dijkstra (int star,int end) {node Temp;int x=inf;temp.pos=star,temp.time=0; S.push (temp); while (! S.empty ()) {node temp1;temp1=temp=s.top (); S.pop (); vis[temp.pos]=1;if (temp.pos==end) {x=temp.time;break;} for (int i=min_road;i<=max_road;i++) {if (!vis[i]&&map[temp.pos][i]<1000000) {Temp.time=map[temp.pos] [I]+temp.time;temp.pos=i; S.push (temp);} TEMP=TEMP1;}} return x;} int main () {int t,s,d;while (scanf ("%d%d", &t,&s,&d)!=eof) {Min_road=inf,max_road=-inf;memset (map,100, sizeof (map)); for (int i=0;i<t;i++) {int a,b,time;scanf ("%d%d%d", &a,&b,&time); if (map[a][b]>time)/ /There may be multiple paths to pick the smallest map[a][b]=map[b][a]=time;if (a>max_road)//max_road,min_road just to shorten a little bit of time max_road=a;if (b>max_ Road) Max_roAd=b;if (a<min_road) min_road=a;if (b<min_road) min_road=b;} int Star[1005],min_time=inf;memset (star,0,sizeof (Star)), for (int i=0;i<s;i++) scanf ("%d", &star[i]), and for (int i =0;i<d;i++) {int end;scanf ("%d", &end), for (int. j=0;j<s;j++) {while (! S.empty ()) S.pop (); memset (vis,0,sizeof (VIS)); int Temp=dijkstra (star[j],end); if (min_time>temp) min_time=temp;}} printf ("%d\n", Min_time);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU Travel of 20,661 persons (Dijkstra)

Related Article

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.