Advertisement
newb_ie

LightOJ - Not the Best

Mar 12th, 2021
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxN = 5e3 + 100;
  5. vector<pair<int64_t,int64_t>> adj[maxN];
  6. int64_t dist[2][maxN];
  7.  
  8. void dijkstra (int source,int n,bool yes) {
  9.     for (int i = 1; i <= n; ++i) dist[yes][i] = 1e12;
  10.     dist[yes][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_cost = abs(pq.top().first);
  15.         int64_t node = pq.top().second;
  16.         pq.pop();
  17.         if (dist[yes][node] < node_cost) continue;
  18.         for (pair<int64_t,int64_t> child : adj[node]) {
  19.             if (child.second + node_cost < dist[yes][child.first]) {
  20.                 dist[yes][child.first] = child.second + node_cost;
  21.                 pq.push(make_pair(-dist[yes][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;
  32.      cin >> T;
  33.      for (int test_case = 1; test_case <= T; ++test_case) {
  34.          for (int i = 1; i < maxN; ++i) adj[i].clear();
  35.          int n,m;
  36.          cin >> n >> m;
  37.          vector<pair<pair<int64_t,int64_t>,int64_t>> edges;
  38.          for (int i = 1; i <= m; ++i) {
  39.              int64_t u,v,cost;
  40.              cin >> u >> v >> cost;
  41.              adj[u].push_back(make_pair(v,cost));
  42.              adj[v].push_back(make_pair(u,cost));
  43.              edges.push_back(make_pair(make_pair(u,v),cost));
  44.          }
  45.          dijkstra(1,n,0);
  46.          dijkstra(n,n,1);
  47.          int64_t first_s = dist[0][n];
  48.          int64_t second_s = 1e12;
  49.          for (pair<pair<int64_t,int64_t>,int64_t> e : edges) {
  50.              int64_t current = min(dist[0][e.first.first] + dist[1][e.first.second],dist[1][e.first.first] + dist[0][e.first.second]);
  51.              if (current + e.second > first_s) {
  52.                  second_s = min(second_s,current + e.second);
  53.              }
  54.              second_s = min(second_s,current + (e.second * 3));
  55.          }
  56.          cout << second_s << '\n';
  57.      }
  58.      //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement