Hdoj 2680 Choose the best route

Source: Internet
Author: User

Choose the best routeTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 10782 Accepted Submission (s): 3491

Problem Descriptionone Day, Kiki wants to visit one of her friends. As she is liable to carsickness, she wants to arrive at her friend's home as soon as possible. Now give your a map of the city ' s traffic route, and the stations which is near Kiki's home so, she can take. Suppose Kiki can change the bus at any station. Please find the least time Kiki needs to spend. To make it easy, if the city has n bus stations, the stations would been expressed as an integer 1,2,3...N.
Inputthere is several test cases.
Each case begins with three integers n, m and S, (N<1000,M<20000,1=<S<=N) n stands for the number of bus statio NS in this city and M stands for the number of directed ways between bus stations. (maybe there is several ways between, bus stations.) s stands for the bus station that near Kiki ' s friend's home.
Then follow m lines, each line contains three integers p, q, T (0<t<=1000). means from station p to station Q there is a and it'll costs T minutes.
Then a line with an integer w (0<w<n), means the number of stations Kiki can take at the beginning. Then follows W integers stands for these stations.

Outputthe output contains one line for each data set:the least time Kiki needs to spend, if it's impossible to find such A route, just output "-1".
Sample Input
5 8 51 2 21 5 31 3 42 4 72 5 62 3 53 5 14 5 122 34 3 41 2 31 3 42 3 211

Sample Output
1-1 Test Instructions: Take a second example!                               4 (Number of nodes 1-4) 3 (3 Road) 4 (source location) (starting point) 1 2 3 1 3 4    2 3 2; (3 road) 1; (one end) 1; (End position) Find the shortest way to the end!! Idea: can use Floyd direct all traverse, find out can!! , but too time-consuming, may time out, I write here with Dijkstra: code:
#include <cstdio> #include <cstring> #include <algorithm> #define MAX 0x3f3f3f3fusing namespace std;    int map[1010][1010],d[1010],n,m,s,t;void Dijkstra (int x) {int i,j,min,mark,used[1010]; for (i=1;i<=n;i++) {used[i]=0;//All marked as not accessed d[i]=map[x][i];//all point to Source Point} d[x]=0;        The source point to itself is zero used[x]=1;//the source point is marked for access over for (i=2;i<=n;i++) {Min=max;        Mark=-1; for (j=1;j<=n;j++)//Find the nearest point {if (!used[j]&&d[j]<min) {Min=d[j]                ;        mark=j;//record Subscript}} if (Mark==-1) break; used[mark]=1;//marks the Subscript node as accessed for (j=1;j<=n;j++) {if (!used[j]&&d[j]>d[mark]+map[mark        ][J])//update path D[J]=D[MARK]+MAP[MARK][J];    }}}int Main () {int a,b,c,i,j;        while (scanf ("%d%d%d", &n,&m,&s)!=eof) {memset (map,max,sizeof (map)); for (i=0;i<m;i++)//Draw diagram {scanf ("%d%d%d ", &a,&b,&c);        if (map[b][a]>c)//Prevent heavy edge map[b][a]=c;        } Dijkstra (s);        int start;        int Mi=max;        scanf ("%d", &t);            for (i=0;i<t;i++) {scanf ("%d", &start);        mi=mi<d[start]?mi:d[start];//to find the shortest distance} if (Mi==max)//Unable to locate the most shorted printf (" -1\n");    else printf ("%d\n", MI); }return 0;}


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

Hdoj 2680 Choose the best route

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.