Choose the best route
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 10690 Accepted Submission (s): 3454
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 idea: SPFA algorithm + reverse map + adjacency table + Queue code:
This question to remember is the inverse of the map! Otherwise it will time out! #include <stdio.h> #include <string.h> #include <queue> #include <iostream> #include < Algorithm> #define INF 0x3f3f3f3fusing namespace Std;int n,m,s;int edgenum;int head[40005];int dis[1005];int vis[1005 ];void init () {Edgenum=0;memset (head,-1,sizeof (Head));} struct Edge{int from,to,val,next;} Edge[40005];void addedge (int u,int v,int w) {Edge e={u,v,w,head[u]};edge[edgenum]=e;head[u]=edgenum++;} void Getmap () {int a,b,c;for (int i=1;i<=m;i++)//This topic to remember is the inverse of the map! {scanf ("%d%d%d", &a,&b,&c); Addedge (b,a,c);}} void Spfa () {Queue<int>q;q.push (s); memset (dis,inf,sizeof (dis)); memset (vis,0,sizeof (VIS));d is[s]=0;vis[s]=1 ; while (!q.empty ()) {int U=q.front (); Q.pop (); vis[u]=0;for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].to;if ( Dis[v]>dis[u]+edge[i].val) {dis[v]=dis[u]+edge[i].val;if (vis[v]==0) {Vis[v]=1;q.push (v);}}}} int main () {while (scanf ("%d%d%d", &n,&m,&s)!=eof) {init (); Getmap (); SPFA (); int sx,min=inf,x;scanf ("%d", &SX);(int i=1;i<=sx;i++) {scanf ("%d", &x); min=min>dis[x]?dis[x]:min;} if (min==inf)//equals is not an assignment number! printf (" -1\n"), Else printf ("%d\n", Min);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 2680 Choose The best route <SPFA algorithm + reverse build >