Choose the best routeTime

** limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)**

Total submission (s): 10619 Accepted Submission (s): 3423

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 Code 1: Reverse consideration; ask Dijkstra once. 530MS.#include <stdio.h> #include <string.h> #include <algorithm> #define INF 0x3f3f3fusing namespace std; int map[1010][1010],low[1010],vis[1010],time;int n;int init () {for (int. i=1;i<=n;i++) {for (int j=1;j<=n ; j + +) {if (i==j) map[i][j]=0; else Map[i][j]=inf; }}}void Dijkstra (int x) {int min,i,j,next; memset (vis,0,sizeof (VIS)); for (i=1;i<=n;i++) {low[i]=map[x][i]; } vis[x]=1; for (i=2;i<=n;i++) {min=inf; for (j=1;j<=n;j++) {if (!vis[j]&&min>low[j]) {min=low[j]; Next=j; }} if (Min==inf) break; Vis[next]=1; for (j=1;j<=n;j++) {if (!vis[j]&&low[j]>low[next]+map[next][j]) { LOW[J]=LOW[NEXT]+MAP[NEXT][J]; }}}}int Main () {int I,a,b,c,w,m,s,beg; while (scanF ("%d%d%d", &n,&m,&s)!=eof) {time=inf; Init (); for (i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&c); if (map[b][a]>c)//reverse, two the least time between stations. Map[b][a]=c; } Dijkstra (s); scanf ("%d", &w); for (i=0;i<w;i++) {scanf ("%d", &beg); if (Time>low[beg]) Time=low[beg]; } if (Time==inf) printf (" -1\n"); else printf ("%d\n", time); } return 0;}

Code Listing 2: Forward consideration, Dijkstra once. 452MS.#include <stdio.h> #include <string.h> #include <algorithm> #define INF 0x3f3f3fusing namespace std; int map[1010][1010],low[1010],vis[1010];int n,m,s;int init () {for (int. i=0;i<=n;i++) {for (int j=0;j<=n; J + +) {if (i==j) map[i][j]=0; else Map[i][j]=inf; }}}void Dijkstra (int x) {int i,j,min,next; memset (vis,0,sizeof (VIS)); for (i=0;i<=n;i++) {low[i]=map[x][i]; } vis[x]=1; for (i=0;i<=n;i++) {min=inf; for (j=0;j<=n;j++) {if (!vis[j]&&min>low[j]) {min=low[j]; Next=j; }} vis[next]=1; if (Min==inf) {break; } for (j=0;j<=n;j++) {if (!vis[j]&&low[j]>low[next]+map[next][j]) { LOW[J]=LOW[NEXT]+MAP[NEXT][J]; }}}}int Main () {int i,a,b,c,w,beg while (scanf ("%d%d%d", &n,&m,&s)!=eof) {init (); for (i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&c); if (map[a][b]>c) {map[a][b]=c; }} scanf ("%d", &w); for (i=0;i<w;i++) {scanf ("%d", &beg); map[0][beg]=0; The distance from 0 to the starting point is 0;} dijkstra (0); if (low[s]==inf) printf (" -1\n"); else printf ("%d\n", Low[s]); } return 0;}

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

Hdoj 2680 Choose the best route