Submission #1339918

#TimeUsernameProblemLanguageResultExecution timeMemory
1339918aykhnCommuter Pass (JOI18_commuter_pass)C++17
16 / 100
225 ms32584 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[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()) {
    array<int, 2> f = pq.top();
    pq.pop();
    if (f[0] > dist[f[1]]) continue;
    for (const array<int, 2> &v : adj[f[1]]) {
      if (dist[v[0]] > f[0] + v[1]) {
        dist[v[0]] = f[0] + 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[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()) {
    int f = pq.top()[1];
    int w = pq.top()[0];
    pq.pop();
    if (w > d[f]) continue;
    for (const array<int, 2> &v : adj[f]) {
      if (d[v[0]] > w + v[1]) {
        d[v[0]] = w + v[1];
        par[v[0]].clear();
        par[v[0]].push_back(f);
        pq.push({d[v[0]], v[0]});
      }
      else if (d[v[0]] == w + v[1]) par[v[0]].push_back(f);
    }
  }
  build(t);
  fill(used + 1, used + n + 1, 0);
  tp(s);
  reverse(o.begin(), o.end());
  int res = inf;
  for (int x : o) {
    mn[x] = min(mn[x], dist[0][x]);
    res = min(res, mn[x] + dist[1][x]);
    for (int v : g[x]) mn[v] = min(mn[v], mn[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...