結果

問題 No.3810 Binary Shortest Path Query
コンテスト
ユーザー Kude
提出日時 2026-11-20 22:17:48
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 164 ms / 2,000 ms
+ 142µs
コード長 2,413 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,745 ms
コンパイル使用メモリ 370,620 KB
実行使用メモリ 32,292 KB
最終ジャッジ日時 2026-11-20 22:18:02
合計ジャッジ時間 12,572 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, q;
  cin >> n >> m >> q;
  dsu uf(n);
  VL s(n);
  rep(i, n) cin >> s[i];
  vector<tuple<int, int, ll>> es;
  for (int i = 1; i <= m; i++) {
    int u, v;
    ll w;
    cin >> u >> v >> w;
    u--, v--;
    if (w == 0) {
      u = uf.leader(u), v = uf.leader(v);
      s[uf.merge(u, v)] = min(s[u], s[v]);
    } else if (i <= 61 && __int128(w) << i < (ll)2e18) {
      es.emplace_back(u, v, w << i);
    }
  }
  vector<vector<pair<int, ll>>> to(n + 1);
  for (auto [u, v, w] : es) {
    u = uf.leader(u);
    v = uf.leader(v);
    to[u].emplace_back(v, w);
    to[v].emplace_back(u, w);
  }
  rep(i, n) if (uf.leader(i) == i) to[i].emplace_back(n, s[i]);
  vector<vector<pair<int, ll>>> dist(n);
  struct E {
    int v;
    ll d;
    bool operator<(const E& rhs) const { return d > rhs.d; }
  };
  constexpr long long INF = 2002003004005006007;
  priority_queue<E> pq;
  VL t(n + 1, INF);
  VI visited;
  rep(u, n) if (uf.leader(u) == u) {
    pq.emplace(u, 0);
    t[u] = 0;
    while (pq.size()) {
      auto [u, d] = pq.top(); pq.pop();
      if (t[u] != d) continue;
      visited.emplace_back(u);
      for (auto [v, w] : to[u]) if (chmin(t[v], d + w)) pq.emplace(v, d + w);
    }
    sort(all(visited));
    for (int v : visited) dist[u].emplace_back(v, t[v]), t[v] = INF;
    visited.clear();
  }
  rep(_, q) {
    int a, b;
    cin >> a >> b;
    a = uf.leader(a - 1), b = uf.leader(b - 1);
    ll ans = dist[a].back().second + dist[b].back().second;
    if (auto it = ranges::lower_bound(dist[a], b, {}, &pair<int, ll>::first); it->first == b) chmin(ans, it->second);
    cout << ans << '\n';
  }
}
0