Hdoj-2066-a man's Journey (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): 23396 Accepted Submission (s): 8142

problem DescriptionAlthough the grass is a road fetish (that is, in Hangzhou electric for more than a year, incredibly still will be lost in the campus, Khan ~), but the grass is still very fond of travel, because in the journey will meet a lot of people (Prince Charming, ^0^), a lot of things, but also to enrich their experience, but also to see 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 ~).  
InputThe 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 son of the city adjacent to the cities have s, where the grass wants to go 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.  
Outputthe shortest time the 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
Thinking of solving problemsThis is a problem to find the shortest path, with the common shortest path is different from the problem has a number of starting points, you can use for the loop Dijkstra to answer. Might worry that the value of the map array has changed since the first Dijkstra? Will it be affected the second time? The answer is no, we only need the initial now and OK array, the map array is only the storage distance, if the first time after the value of Map[i][j] is 2,and the second Map[i][j] value is 3, obviously we will choose 2,3 directly ruled out. the map array after the For loop Dijkstra is the shortest distance to all locations, regardless of the location from which to start. Cause of errorOJ shows Runtime error (access_violation), which causes this error to be two, (1) The array is small, (2) The array is out of bounds Code
#include <stdio.h>int map[1100][1100];int ok[1100];int now[1100];int sta[1100];int end[1100]; #define MAX 1007int Main () {int t,s,d;int i,j,k;int nowlen,nowx;int a,b,l;int ans;while (scanf ("%d%d%d", &t,&s,&d)!=EOF) {for (i =1;i<=max;i++) for (j=1;j<=max;j++) Map[i][j]=max;//map initial infinity for (i=1;i<=t;i++) {scanf ("%d%d%d", &AMP;A,&A MP;B,&AMP;L); if (map[a][b]>l) map[a][b]=map[b][a]=l;} Updates the given distance for (i=1;i<=s;i++) scanf ("%d", &sta[i]), the for (i=1;i<=d;i++) scanf ("%d", &end[i]), and for (i=1;i< =s;i++) {//for loop Dijkstra for (j=1;j<=max;j++) {now[j]=max;ok[j]=0;}    now[sta[i]]=0;ok[sta[i]]=1;//initial now and OK for (k=1;k<=max;k++) {Nowlen=max;    for (j=1;j<=max;j++) {if (!ok[j]&&nowlen>map[sta[i]][j]) {nowlen=map[sta[i]][j];    Nowx=j;    }//find the smallest distance in the distance now[nowx]=nowlen;    Ok[nowx]=1; for (j=1;j<=max;j++) {if (!ok[j]&&map[sta[i]][j]>now[nowx]+map[nowx][j]) map[sta[i]][j]=now[nowx ]+MAP[NOWX][J];    }//update other points to Origin distance}}ans=max;for (i=1;i<=s;i++) for (j=1;j<=d;j++) if (Map[sta[i]][end[j]]<ans) ans=map[sta[i]][end[j]];//Find the smallest printf ("%d\n", ans); return 0;}


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

Hdoj-2066-a man's Journey (Dijkstra)

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.