Description
One 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.
Input
There 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.
Output
The output contains one line for each data set:the least time Kiki needs to spend, if it's impossible to find such a rou TE, 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: give you traffic routes and Kiki home can arrive at the station, let you find Kiki arrive at the shortest point of destination, if cannot reach, on output-1;
Idea: For a multi-starting point of the problem, you can simplify the starting point as a starting point, that is: the Kiki home as a real starting point, and then his home to each can reach the starting point of the distance as 0, so that can shorten the complexity of time. For multi-origin and multi-end situations, multiple starting points can be connected to the same imaginary starting point, and the distance is set to 0, and the end point is the same.
Note: The subject indicates that the traffic route is directed, that is: the map
The code is as follows:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<cmath>#include<cstdlib>UsingNamespace Std;Constint INF=0x3f3f3f3f;Constint N=1010;INT MP[N][n];int dis[N];int Vis[N];int m;int n;IntDijstra(){Memset(Dis,0x3f,sizeof(Dis));Memset(Vis,0,sizeof(Vis)); Dis[0]=0;For(int I=1; I<=n; I++){int k=0;int Mini=inf;For(Int J=1; j<=n; j++){If(!vis[j]&&mini>dis[j]) Mini=dis[k=j];} Vis[k]=1;If(k==m)return dis[M];For(Int J=1; j<=n; j++){If(Vis[j]|| Mp[k][j]==inf)Continue; Dis[j]=Min(Dis[j],dis[k]+mp[k][j]);}}return dis[M];}IntMain(){int s;There are several roads that have been repaired.While(~scanf("%d%d%d", &n, &s, &m))The end is M, the farthest point is n{Memset(MPInf,sizeof(MP));While(s--){int aBC;scanf("%d%d%d", &a, &b, &c);If(MP[A][b]>c) MP[A][b]=c;}int D;Number of start pointsscanf("%d", &d);While(d--){int x;Starting pointscanf("%d", &x); Mp[0][x]=0;will be able to do the starting point is connected to the ' 0 ', and are assigned a value of 0, does not affect the subsequent operation, to ensure that there is only one starting point ' 0 '}int k=dijstra () ; if (K==infprintf ( "-1 \n "); else printf ( "%d\n" , Dijstra return 0;< Span class= "Sh-cbracket" >
HDU 2680 (Shortest path from one endpoint to another)