题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=2680
Choose the best route
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 you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.
Input
There are 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 stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two 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 way and it will 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 route ,just output “-1”.
Sample Input
5 8 5
1 2 2
1 5 3
1 3 4
2 4 7
2 5 6
2 3 5
3 5 1
4 5 1
2
2 3
4 3 4
1 2 3
1 3 4
2 3 2
1
1
Sample Output
1
-1
最短路。。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<queue> 8 #include<map> 9 using std::min; 10 using std::cin; 11 using std::cout; 12 using std::endl; 13 using std::find; 14 using std::sort; 15 using std::pair; 16 using std::vector; 17 using std::multimap; 18 using std::priority_queue; 19 #define pb(e) push_back(e) 20 #define sz(c) (int)(c).size() 21 #define mp(a, b) make_pair(a, b) 22 #define all(c) (c).begin(), (c).end() 23 #define iter(c) decltype((c).begin()) 24 #define cls(arr,val) memset(arr,val,sizeof(arr)) 25 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 26 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 28 const int N = 20010; 29 const int INF = 0x3f3f3f3f; 30 typedef unsigned long long ull; 31 struct P { 32 int w, v; 33 P(int i = 0, int j = 0) :w(i), v(j) {} 34 inline bool operator<(const P &a) const { 35 return w > a.w; 36 } 37 }; 38 struct Node { int to, w, next; }; 39 struct Dijkstra { 40 Node G[N]; 41 int tot, dist[N], head[N]; 42 inline void init() { 43 tot = 0, cls(dist, 0x3f), cls(head, -1); 44 } 45 inline void add_edge(int u, int v, int w) { 46 G[tot] = { v, w, head[u] }; head[u] = tot++; 47 } 48 inline void built(int m) { 49 int u, v, w; 50 while (m--) { 51 scanf("%d %d %d", &u, &v, &w); 52 add_edge(v, u, w); 53 } 54 } 55 inline void dijkstra(int s) { 56 priority_queue<P> q; 57 q.push(P(0, s)); 58 dist[s] = 0; 59 while (!q.empty()) { 60 P t = q.top(); q.pop(); 61 int u = t.v; 62 if (dist[u] < t.w) continue; 63 for (int i = head[u]; ~i; i = G[i].next) { 64 int &w = dist[G[i].to]; 65 if (w > dist[u] + G[i].w) { 66 w = dist[u] + G[i].w; 67 q.push(P(w, G[i].to)); 68 } 69 } 70 } 71 } 72 inline void work(int s) { 73 dijkstra(s); 74 int k, v, ans = INF; 75 scanf("%d", &k); 76 while (k--) { 77 scanf("%d", &v); 78 ans = min(ans, dist[v]); 79 } 80 printf("%d ", ans == INF ? -1 : ans); 81 } 82 }go; 83 int main() { 84 #ifdef LOCAL 85 freopen("in.txt", "r", stdin); 86 freopen("out.txt", "w+", stdout); 87 #endif 88 int n, m, s; 89 while (~scanf("%d %d %d", &n, &m, &s)) { 90 go.init(); 91 go.built(m); 92 go.work(s); 93 } 94 return 0; 95 }