Dijkstra + Heap 迪杰斯特拉算法加堆优化。 马克一下,好东西齐分享,也收集改进方案~ 题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680

Choose the best route

Problem 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 代码:http://paste.ubuntu.com/7315517/
  1. #include <cstdio>
  2. #include <vector>
  3. #include <string>
  4. #include <queue>
  5. using namespace std;
  6. const int N = 1e3, M = 2e4;
  7. struct edge {
  8.     int x, t;
  9.     edge(int tx, int tt): x(tx), t(tt) {}
  10. };
  11. vector <edge> V[N + 5];
  12. priority_queue <edge> Q;
  13. bool vis[N + 5];
  14. bool operator < (const edge &a, const edge &b) {
  15.     return a.t > b.t;
  16. }
  17. int dij(int s, int e) {
  18.     memset(vis, truesizeof(vis));
  19.     while (!Q.empty()) Q.pop();
  20.     Q.push(edge(s,0));
  21.     while (!Q.empty()) {
  22.         edge u = Q.top();
  23.         Q.pop();
  24.         if (!vis[u.x]) continue;
  25.         if (u.x == e) return u.t;
  26.         vis[u.x] = false;
  27.         for (vector <edge>::iterator i = V[u.x].begin(); i != V[u.x].end(); ++i)
  28.             if (vis[i->x]) Q.push(edge(i->x, u.t + i->t));
  29.     }
  30.     return -1;
  31. }
  32. int main() {
  33.     int n, m, s;
  34.     while (scanf("%d%d%d", &n, &m, &s) != EOF) {
  35.         for (int i = 0; i <= n; ++i)
  36.             while (!V[i].empty()) V[i].pop_back();
  37.         while (m--) {
  38.             int u, v, t;
  39.             scanf("%d%d%d", &u, &v, &t);
  40.             V[u].push_back(edge(v, t));
  41.         }
  42.         int k;
  43.         scanf("%d", &k);
  44.         while (k--) {
  45.             int p;
  46.             scanf("%d", &p);
  47.             V[0].push_back(edge(p, 0));
  48.         }
  49.         printf("%d\n", dij(0, s));
  50.     }
  51.     return 0;
  52. }
转载保留版权:http://haipz.com/blog/i/1375 - 海胖博客