Advertisement
newb_ie

CSES - Flight Discount

Mar 13th, 2021
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxN = 1e5 + 100;
  5. vector<pair<int64_t,int64_t>> adj[2][maxN];
  6. int64_t dist[2][maxN];
  7.  
  8. void dijkstra (int64_t source,int64_t n,int64_t type) {
  9.     for (int i = 1; i <= n; ++i) dist[type][i] = LLONG_MAX;
  10.     dist[type][source] = 0;
  11.     priority_queue<pair<int64_t,int64_t>> pq;
  12.     pq.push(make_pair(0,source));
  13.     while (!pq.empty()) {
  14.         int64_t node = pq.top().second;
  15.         int64_t node_cost = abs(pq.top().first);
  16.         pq.pop();
  17.         if (dist[type][node] < node_cost) continue;
  18.         for (pair<int64_t,int64_t> child : adj[type][node]) {
  19.             if (dist[type][child.first] > node_cost + child.second) {
  20.                 dist[type][child.first] = node_cost + child.second;
  21.                 pq.push(make_pair(-dist[type][child.first],child.first));
  22.             }
  23.         }
  24.     }
  25. }
  26.  
  27. int main () {
  28.      ios::sync_with_stdio(false);
  29.      cin.tie(nullptr);
  30.      cout.tie(nullptr);
  31.      int T = 1;
  32.      //~ cin >> T;
  33.      for (int test_case = 1; test_case <= T; ++test_case) {
  34.          int n,m;
  35.          cin >> n >> m;
  36.          vector<pair<pair<int64_t,int64_t>,int64_t>> edges;
  37.          for (int i = 1; i <= m; ++i) {
  38.              int64_t u,v,c;
  39.              cin >> u >> v >> c;
  40.              adj[0][u].push_back(make_pair(v,c));
  41.              adj[1][v].push_back(make_pair(u,c));
  42.              edges.push_back(make_pair(make_pair(u,v),c));
  43.          }
  44.          dijkstra(1,n,0);
  45.          dijkstra(n,n,1);
  46.          int64_t res = LLONG_MAX;
  47.          for (pair<pair<int64_t,int64_t>,int64_t> e : edges) {
  48.              int64_t current_edge_cost = e.second;
  49.              int64_t first = e.first.first;
  50.              int64_t second = e.first.second;
  51.              int64_t cost_x = dist[0][first];
  52.              int64_t cost_y = dist[1][second];
  53.              if (cost_x >= LLONG_MAX or cost_y >= LLONG_MAX) {
  54.                  continue;
  55.              }
  56.              res = min(res,cost_x + cost_y + (current_edge_cost / 2));
  57.          }
  58.          cout << res << '\n';
  59.      }
  60.      //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement