Choose the best routeTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 7671 Accepted Submission (s): 2524
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
AC Code:
#include <cstdio> #include <cstring> #include <algorithm> #define INF 0x7f7f7f7fusing namespace std; const int MAX = 1005;int N, m, S;int Map[max][max], Dis[max], vis[max];void Dijk (int s) {for (int i=1; i<=n; i++) dis[i] = Map[s][i];vis[s] = 1; Dis[s] = 0;for (int i=1; i<n; i++) {int min = INF, pos;for (int j = 1; J <= N; j + +) if (!vis[j] && dis[j] < mi n) min = Dis[pos = j];if (min = = INF) Break;vis[pos] = 1;for (int j=1; j<=n; J + +) if (!vis[j] && Dis[pos] + map[pos ][J] < Dis[j]) dis[j] = Dis[pos] + map[pos][j];}} int main () {while (scanf ("%d%d%d", &n, &m, &s)! = EOF) {memset (Vis, 0, sizeof (VIS)), memset (map, 0x7f, sizeof (MA p)); while (m--) {int x, Y, w;scanf ("%d%d%d", &x, &y, &w); if (W < map[y][x]) map[y][x] = w;//attention is to the direction, the opposition to the}dijk (s); int A, b, ans = inf;scanf ("%d", &b), for (int i=1; i<=b; i++) {scanf ("%d", &a); ans = min (ans, dis[a]);} if (ans! = INF) printf ("%d\n", ans), else printf (" -1\n");} return 0;}
Hdu-2680-choose the best route (Classic Shortest path problem Dijkstra algorithm!!) )