Submission #756856

#TimeUsernameProblemLanguageResultExecution timeMemory
756856NeroZeinCommuter Pass (JOI18_commuter_pass)C++17
0 / 100
805 ms16872 KiB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

using T = pair<int, int>;

const int N = 100005;

int n, m;
vector<pair<int, int>> g[N]; 

vector<long long> dijk (int src) {
  priority_queue<T, vector<T>, greater<T>> pq; 
  vector<long long> cost(n + 1, 1e15); 
  pq.emplace(0, src);
  cost[src] = 0; 
  while (!pq.empty()) {
    auto [c, v] = pq.top();
    pq.pop();
    if (cost[v] < c) {
      continue; 
    }
    for (auto [u, w] : g[v]) {
      if (c + w < cost[u]) {
        cost[u] = c + w; 
        pq.emplace(cost[u], u); 
      }
    }
  }
  return cost; 
}

signed main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m;
  int s, t;
  cin >> s >> t;
  int uu, vv;
  cin >> uu >> vv; 
  for(int i = 0; i < m; ++i) {
    int x, y, w;
    cin >> x >> y >> w;
    g[x].emplace_back(y, w);
    g[y].emplace_back(x, w);
  }
  auto cu = dijk(uu);
  auto cv = dijk(vv);
  long long ans = LLONG_MAX; 
  for (int it = 0; it < 2; ++it) {
    priority_queue<T, vector<T>, greater<T>> pq; 
    auto ct = dijk(t);
    vector<long long> cs(n + 1, 1e15); 
    vector<long long> dp(n + 1, 1e15); 
    dp[s] = cu[s];
    pq.emplace(0, s);
    cs[s] = 0; 
    while (!pq.empty()) {
      auto [c, v] = pq.top();
      pq.pop();
      if (cs[v] < c) {
        continue; 
      }
      for (auto [u, w] : g[v]) {
        if (c + w < cs[u]) {
          cs[u] = c + w;
          pq.emplace(cs[u], u); 
        }
        if (c + w == cs[u]) {
          dp[u] = min({dp[u], dp[v], cu[u]});
        }
      }
    }
    for (int i = 1; i <= n; ++i) {
      if (ct[i] + cs[i] == ct[s]) {
        ans = min(ans, cv[i] + dp[i]); //u -> x -> i -> v
      }
    }
    swap(s, t); 
  }
  cout << ans << '\n';
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...