Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. /** Includes **/
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <chrono>
  6. #include <vector>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10.  
  11. using namespace std;
  12.  
  13. /** Shortcuts **/
  14. #define fi first
  15. #define se second
  16. #define pb push_back
  17. #define pf push_front
  18. #define Pb pop_back
  19. #define Pf pop_front
  20. #define mp make_pair
  21. #define INF (int32)1e18
  22.  
  23. typedef long int int32;
  24. typedef unsigned long int uint32;
  25. typedef long long int int64;
  26. typedef unsigned long long int uint64;
  27.  
  28. typedef long long LL;
  29. typedef long double LD;
  30.  
  31. /** Globals **/
  32.  
  33. vector<LL> dijjjkstra(int s, int n, vector<vector<pair<int, LL>>> &edges) {
  34.     vector<LL> d(n, INF);
  35.     d[s] = 0;
  36.     priority_queue<pair<LL, int>> q;
  37.     q.push({0, s});
  38.  
  39.     while (!q.empty()) {
  40.         int v = q.top().se;
  41.         if (-q.top().fi > d[v]) {
  42.             continue;
  43.         }
  44.  
  45.         for (int j = 0; j < edges[v].size(); ++j) {
  46.             int u = edges[v][j].fi;
  47.             LL weight = edges[v][j].se;
  48.  
  49.             if (d[u] > d[v] + weight) {
  50.                 d[u] = d[v] + weight;
  51.                 q.push({-d[u], u});
  52.             }
  53.         }
  54.  
  55.         q.pop();
  56.     }
  57.     return d;
  58. }
  59.  
  60. void solve() {
  61.     int n, m;
  62.     cin >> n >> m;
  63.     vector<vector<pair<int, LL>>> edges(n);
  64.  
  65.     for (int i = 0; i < m; ++i) {
  66.         int a, b;
  67.         LL w;
  68.         cin >> a >> b >> w;
  69.         a--, b--;
  70.         edges[a].emplace_back(b, w);
  71.     }
  72.  
  73.     int a, b, c;
  74.     cin >> a >> b >> c;
  75.     a--, b--, c--;
  76.  
  77.     auto ansa = dijjjkstra(a, n, edges);
  78.     auto ansb = dijjjkstra(b, n, edges);
  79.     auto ansc = dijjjkstra(c, n, edges);
  80.  
  81.     LL ab = ansa[b];
  82.     LL ac = ansa[c];
  83.     LL bc = ansb[c];
  84.  
  85.     LL ba = ansb[a];
  86.     LL cb = ansc[b];
  87.     LL ca = ansc[a];
  88.  
  89.     LL minimum1 = min(ab + bc, min(bc + ca, ca + ab));
  90.     LL minimum2 = min(ab + ac, min(ba+ bc, cb + ca));
  91.     LL minimum3 = min(ca + ba, min(ac + cb, ba + ac));
  92.     LL minimum4 = min(ba + ca, min(ab + cb, ac + bc));
  93.     LL minimum = min(min(minimum1, minimum2),
  94.                       min(minimum3, minimum4));
  95.  
  96.     if (minimum >= INF) {
  97.         cout << "-1\n";
  98.     } else {
  99.         cout << minimum << "\n";
  100.     }
  101. }
  102.  
  103.  
  104. int main() {
  105.     auto start = std::chrono::steady_clock::now();
  106.     std::ios_base::sync_with_stdio(false);
  107.  
  108.     cin.tie(nullptr);
  109.     cout.tie(nullptr);
  110.  
  111. //    freopen(".in", "r", stdin);
  112. //    freopen(".out", "w", stdout);
  113.  
  114.     solve();
  115.  
  116.     auto finish = chrono::steady_clock::now();
  117.     auto elapsed_ms = chrono::duration_cast<std::chrono::milliseconds>(finish - start);
  118.     cerr << "Elapsed: " << elapsed_ms.count() << " ms\n";
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement