HDU 2680 Choose the best route (Dijkstra & Reverse chart)

Source: Internet
Author: User

Choose the best route
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 7764 Accepted Submission (s): 2581


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

Authordandelion
Source2009 Zhejiang University Computer Research and Examination (machine test part)--Full true simulation




Test instructions: A person, to take the bus to work. Always have n stations, M route, known he can from the W stations to travel, to arrive at the destination s the minimum time spent. If not, then output-1.


Parse: Dijkstra algorithm, but the direct use of W-Dijkstra, will time out. Here are two ideas:

1. Set the person's location to site 0, then connect the site 0 and the W starting point, which is equivalent to 0 from his home to these sites. Then use Dijkstra, output D[S].

2. Using reverse graph, because there can be multiple starting points, but there is only one end point, so the reverse graph, the equivalent of only a starting point, a lot of endpoints, so long as the use of Dijkstra, and then output the minimum value in the W starting point.

PS: Be sure to note that the bus line is one-way, must be built to map!!!



AC Code:


Idea One

#include <cstdio> #include <iostream> #include <algorithm> #include <cstring>using namespace STD; #define INF 1e7 + 2const int maxn = 5;int N, m;int W[MAXN][MAXN], D[MAXN], v[maxn];void Dijkstra (int s) {mem    Set (V, 0, sizeof (v));    for (int i=0; i<=n; i++) d[i] = W[s][i];    D[s] = 0;    V[s] = 1;        for (int i=0; i<=n; i++) {int x, m = INF;        for (int y=0; y<=n; y++) if (!v[y] && d[y] <= m) m = D[x = y];        if (m = = INF) break;        V[X] = 1;    for (int y=0; y<=n; y++) D[y] = min (D[y], d[x] + w[x][y]);    }}int Main () {#ifdef sxk freopen ("In.txt", "R", stdin);    #endif//sxk int x, y, z, S, t; while (scanf ("%d%d%d", &n, &m, &s)!=eof) {for (Int. i=0; i<=n; i++) for (int j=0; j<=n; j+        +) W[i][j] = (i==j? 0:inf);            for (int i=0; i<m; i++) {scanf ("%d%d%d", &x, &y, &z);        W[x][y] = min (w[x][y], z); }       scanf ("%d", &t);            while (t--) {scanf ("%d", &x);               W[0][x] = 0;        } dijkstra (0);    printf ("%d\n", d[s] = = INF -1:d[s]); } return 0;}




Two ideas

#include <cstdio> #include <iostream> #include <algorithm> #include <cstring>using namespace STD; #define INF 1e7 + 2const int maxn = 5;int N, m;int W[MAXN][MAXN], D[MAXN], v[maxn];void Dijkstra (int s) {mem    Set (V, 0, sizeof (v));    for (int i=0; i<=n; i++) d[i] = W[s][i];    D[s] = 0;    V[s] = 1;        for (int i=0; i<=n; i++) {int x, m = INF;        for (int y=0; y<=n; y++) if (!v[y] && d[y] <= m) m = D[x = y];        if (m = = INF) break;        V[X] = 1;    for (int y=0; y<=n; y++) D[y] = min (D[y], d[x] + w[x][y]);    }}int Main () {#ifdef sxk freopen ("In.txt", "R", stdin);    #endif//sxk int x, y, z, S, t; while (scanf ("%d%d%d", &n, &m, &s)!=eof) {for (Int. i=0; i<=n; i++) for (int j=0; j<=n; j+        +) W[i][j] = (i==j? 0:inf);            for (int i=0; i<m; i++) {scanf ("%d%d%d", &x, &y, &z);       W[y][x] = min (w[y][x], z); SecretariatReverse Graph} Dijkstra (s);        scanf ("%d", &t);        int ans = INF;            while (t--) {scanf ("%d", &x);           ans = min (ans, d[x]);    Find the lowest value} printf ("%d\n", ans = = INF) -1:ans); } return 0;}







HDU 2680 Choose the best route (Dijkstra & Reverse chart)

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.