halfo

(Really) Second Best Shortest Path - LOJ 1099

Aug 4th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. //{ ************[       Template       ]************
  2. using namespace std;
  3. //{ ************[      C headers       ]************
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <cstring>
  8. #include <climits>
  9. #include <cfloat>
  10. #include <cctype>
  11. #include <cassert>
  12. #include <ctime>
  13. //}
  14. //{ ************[     C++ headers      ]************
  15. #include <iostream>
  16. #include <iomanip>
  17. #include <sstream>
  18. #include <algorithm>
  19. #include <numeric>
  20. #include <utility>
  21. #include <string>
  22. #include <stack>
  23. #include <queue>
  24. #include <deque>
  25. #include <vector>
  26. #include <set>
  27. #include <map>
  28. #include <list>
  29. #include <complex>
  30. //{ ************[     Test Report 1    ]************
  31. // #include <tr1/regex>
  32. // #include <tr1/unordered_set>
  33. // #include <tr1/unordered_map>
  34. //}
  35. //}
  36. //{ ************[        Loops         ]************
  37. #define forab(i, a, b)          for (__typeof(b) i = (a); i <= (b); ++i)
  38. #define rep(i, n)       forab (i, 0, (n) - 1)
  39. #define For(i, n)       forab (i, 1, n)
  40. #define rofba(i, a, b)          for (__typeof(b) i = (b); i >= (a); --i)
  41. #define per(i, n)       rofba (i, 0, (n) - 1)
  42. #define rof(i, n)       rofba (i, 1, n)
  43. #define forstl(i, s)            for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
  44. //}
  45. //{ ************[   Floating points    ]************
  46. #define EPS         DBL_EPSILON
  47. #define abs(x)          (((x) < 0) ? - (x) : (x))
  48. #define zero(x)         (abs (x) < EPS)
  49. #define equal(a,b)      (zero ((a) - (b)))
  50. #define PI          2 * acos (0.0)
  51. //}
  52. //{ ************[        Macros        ]************
  53. #define mem(a, i)       memset ((a), i, sizeof ((a)))
  54. #define clr(a)          mem ((a), 0)
  55. #define all(a)          (a).begin (), (a).end ()
  56. #define pb          push_back
  57.  
  58. template <class T, class U> inline T max (T &a, U &b) { return a > b ? a : b; }
  59. template <class T, class U> inline T min (T &a, U &b) { return a < b ? a : b; }
  60. static struct _ { ios_base :: Init Init; _ () { cin.sync_with_stdio (false); cin.tie (false); } } _;
  61. //}
  62. //{ ************[  Typedefs && Consts  ]************
  63. typedef long long int64;
  64. typedef unsigned long long int64u;
  65.  
  66. const int MAX_N = int (5e3) + 7;
  67. const int MOD   = int (1e9) + 7;
  68. //}
  69. //}
  70.  
  71. int best, sBest, s [MAX_N], d [MAX_N];
  72. vector <pair <int, int> > adjList [MAX_N];
  73.  
  74. priority_queue <int, vector <int>, greater <int> > Q;
  75.  
  76. void dijkstra (int source, int cost [MAX_N]) {
  77.     fill (cost, cost + MAX_N, 1 << 22);
  78.     cost [source] = 0;
  79.     Q.push (source);
  80.  
  81.     while (!Q.empty ()) {
  82.         int u = Q.top ();
  83.         Q.pop ();
  84.         forstl (i, adjList [u]) {
  85.             int v = i -> first;
  86.             int w = i -> second;
  87.             if (cost [v] > cost [u] + w) cost [v] = cost [u] + w, Q.push (v);
  88.         }
  89.     }
  90. }
  91.  
  92. void cherry (int val) {
  93.     if (val < best) {
  94.         sBest = best;
  95.         best  = val;
  96.     } else if (val < sBest && val > best) sBest = val;
  97. }
  98.  
  99. int main () {
  100. #ifdef Local
  101.     freopen ("input.txt", "r", stdin);
  102.     // freopen ("output.txt", "w", stdout);
  103. #endif
  104.     int t;
  105.     cin >> t;
  106.  
  107.     For (cs, t) {
  108.         int n, r;
  109.         cin >> n >> r;
  110.  
  111.         rep (i, MAX_N) adjList [i].clear ();
  112.         rep (i, r) {
  113.             int u, v, w;
  114.             cin >> u >> v >> w;
  115.             adjList [u - 1].pb (make_pair (v - 1, w));
  116.             adjList [v - 1].pb (make_pair (u - 1, w));
  117.         }
  118.  
  119.         dijkstra (0, s);
  120.         dijkstra (n - 1, d);
  121.  
  122.         best = sBest = 1 << 22;
  123.         rep (i, n) {
  124.             cherry (s [i] + d [i]);
  125.             forstl (u, adjList [i]) cherry (s [i] + d [u -> first] + u -> second);
  126.         }
  127.  
  128.         cout << "Case " << cs << ": " << sBest << endl;
  129.     }
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment