제출 #1339921

#제출 시각아이디문제언어결과실행 시간메모리
1339921aykhnCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
200 ms33640 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define inf 0x3F3F3F3F3F3F3F3F
 
const int MXN = 1e5 + 5;
 
vector<array<int, 2>> adj[MXN];
vector<int> par[MXN], g[MXN], o;
int dist[2][MXN], d[MXN], mn[2][MXN], used[MXN];
 
void dijk(int s, int* dist) {
  for (int i = 0; i < MXN; i++) dist[i] = inf;
  priority_queue<array<int, 2>, vector<array<int, 2>>, greater<array<int, 2>>> pq;
  dist[s] = 0;
  pq.push({0, s});
  while (!pq.empty()) {
    auto [w, a] = pq.top();
    pq.pop();
    if (w > dist[a]) continue;
    for (const array<int, 2> &v : adj[a]) {
      if (dist[v[0]] > w + v[1]) {
        dist[v[0]] = w + v[1];
        pq.push({dist[v[0]], v[0]});
      }
    }
  }
}
void build(int a) {
  used[a] = 1;
  for (int &v : par[a]) {
    g[v].push_back(a);
    if (used[v]) continue;
    build(v);
  }
}
void tp(int a) {
  used[a] = 1;
  for (int &v : g[a]) {
    if (used[v]) continue;
    tp(v);
  }
  o.push_back(a);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  int n, m, s, t, u, v;
  cin >> n >> m >> s >> t >> u >> v;
  while (m--) {
    int u, v, w;
    cin >> u >> v >> w;
    adj[u].push_back({v, w});
    adj[v].push_back({u, w});
  }
  dijk(u, dist[0]), dijk(v, dist[1]);
  for (int i = 0; i < MXN; i++) d[i] = mn[0][i] = mn[1][i] = inf;
  priority_queue<array<int, 2>, vector<array<int, 2>>, greater<array<int, 2>>> pq;
  d[s] = 0;
  pq.push({0, s});
  while (!pq.empty()) {
    auto [w, a] = pq.top();
    pq.pop();
    if (w > d[a]) continue;
    for (const array<int, 2> &v : adj[a]) {
      if (d[v[0]] > w + v[1]) {
        d[v[0]] = w + v[1];
        par[v[0]].clear();
        par[v[0]].push_back(a);
        pq.push({d[v[0]], v[0]});
      }
      else if (d[v[0]] == w + v[1]) par[v[0]].push_back(a);
    }
  }
  build(t);
  fill(used + 1, used + n + 1, 0);
  tp(s);
  reverse(o.begin(), o.end());
  int res = inf;
  for (int x : o) {
    for (int j = 0; j < 2; j++) {
      mn[j][x] = min(mn[j][x], dist[j][x]);
      res = min(res, mn[j][x] + dist[j ^ 1][x]);
    }
    for (int v : g[x]) {
      for (int j = 0; j < 2; j++) mn[j][v] = min(mn[j][v], mn[j][x]);
    }
  }
  cout << min(dist[0][v], res) << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...