結果

問題 No.3810 Binary Shortest Path Query
コンテスト
ユーザー kwm_t
提出日時 2026-11-20 22:16:13
言語 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  
実行時間 1,046 ms / 2,000 ms
+ 511µs
コード長 5,600 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,781 ms
コンパイル使用メモリ 409,264 KB
実行使用メモリ 49,988 KB
最終ジャッジ日時 2026-11-20 22:16:53
合計ジャッジ時間 29,674 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
// using mint = modint1000000007;
// const int mod = 1000000007;
// using mint = modint998244353;
// const int mod = 998244353;
// const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, l, r) for (int i = (l); i < (r); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, l, r) for (int i = (r)-1; i >= (l); --i)
#define all(x) (x).begin(), (x).end()
#define allR(x) (x).rbegin(), (x).rend()
#define P pair<int, int>
template<typename A, typename B> inline bool chmax(A& a, const B& b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A& a, const B& b) { if (a > b) { a = b; return true; } return false; }
/**
 * @file dijkstra.hpp
 * @brief Dijkstra 法
 * @author kwm_t
 * @see https://github.com/kwm-t/cp-library/blob/main/graph/shortest_path/dijkstra.hpp
 */
#ifndef KWM_T_GRAPH_SHORTEST_PATH_DIJKSTRA_HPP
#define KWM_T_GRAPH_SHORTEST_PATH_DIJKSTRA_HPP

#include <vector>
#include <queue>
#include <limits>
#include <utility>
#include <functional>
#include <algorithm>

 /**
  * @brief ダイクストラ法(単一始点最短路 + 経路復元用情報)
  *
  * 非負重みグラフにおける単一始点最短路を求める。
  * prev を用いることで最短経路の復元が可能。
  *
  * 典型用途:
  *   - 重み付きグラフ(非負)
  *   - グリッド + コスト
  *   - 最短経路 + 経路復元
  *
  * 計算量:
  *   O((V + E) log V)
  *
  * @tparam T 距離型(int, long long など)
  *
  * @param g 隣接リスト (to, cost)
  * @param s 始点
  * @param inf 無限大
  *
  * @return pair(dist, prev)
  *
  * 制約 / 注意:
  *   - 負辺がある場合は使用不可(Bellman-Fordを使う)
  *
  * 使用例:
  *   auto [dist, prev] = dijkstra(g, s, INF);
  *   auto path = restore_path(prev, s, t);
  *
  * verified:
  *   https://atcoder.jp/contests/awc0033/submissions/74392079
  */
namespace kwm_t::graph::shortest_path {

template<typename T>
std::pair<std::vector<T>, std::vector<int>>
dijkstra(const std::vector<std::vector<std::pair<int, T>>>& g, int s, T inf) {
	int n = (int)g.size();

	std::vector<T> dist(n, inf);
	std::vector<int> prev(n, -1);

	using S = std::pair<T, int>;
	std::priority_queue<S, std::vector<S>, std::greater<S>> pq;

	dist[s] = 0;
	pq.emplace(0, s);

	while (!pq.empty()) {
		auto [d, v] = pq.top();
		pq.pop();

		if (d > dist[v]) continue;

		for (auto [to, cost] : g[v]) {
			if (dist[v] > inf - cost) continue; // overflow guard

			T nd = dist[v] + cost;
			if (nd < dist[to]) {
				dist[to] = nd;
				prev[to] = v;
				pq.emplace(nd, to);
			}
		}
	}

	return { dist, prev };
}

/**
 * @brief 経路復元
 * @param prev dijkstra で得た prev
 * @param s 始点
 * @param t 終点
 * @return s -> t の最短経路(存在しない場合は空)
 */
inline std::vector<int>
restore_path(const std::vector<int>& prev, int s, int t) {
	std::vector<int> path;
	for (int v = t; v != -1; v = prev[v]) {
		path.push_back(v);
	}
	std::reverse(path.begin(), path.end());

	if (path.empty() || path[0] != s) return {};
	return path;
}

} // namespace kwm_t::graph::shortest_path

#endif // KWM_T_GRAPH_SHORTEST_PATH_DIJKSTRA_HPP
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int n, m, q; cin >> n >> m >> q;
	dsu uf(n);
	// 2*10^18 < 2^61
	vector<long long>s(n);
	rep(i, n)cin >> s[i];
	long long lim = 2 * LINF;
	vector<int>u, v; vector<long long>w;

	vector g0(n + 1, vector<pair<int, long long>>());
	rep(i, n) g0[n].emplace_back(i, s[i]);
	rep2(i, 1, m + 1) {
		int _u, _v; cin >> _u >> _v;
		_u--, _v--;
		long long _w; cin >> _w;
		if (_w == 0) {
			uf.merge(_u, _v);
			g0[_u].emplace_back(_v, 0);
			g0[_v].emplace_back(_u, 0);
			continue;
		}
		rep(j, i) {
			_w *= 2;
			if (_w > lim)break;
		}
		if (_w > lim)continue;
		v.push_back(_v);
		u.push_back(_u);
		w.push_back(_w);
		g0[_u].emplace_back(_v, _w);
		g0[_v].emplace_back(_u, _w);
	}
	auto [dp0, _] = kwm_t::graph::shortest_path::dijkstra(g0, n, LINF * 2 + 10);

	map<int, int>mp;//leader,index
	rep(i, n) {
		if (uf.leader(i) == i) {
			mp[i] = mp.size();
		}
	}
	int sz = mp.size();
	vector g(sz, vector < pair<int, long long>>());
	{
		int m = u.size();
		rep(i, m) {
			if (uf.same(u[i], v[i]))continue;
			int ul = uf.leader(u[i]);
			int vl = uf.leader(v[i]);
			int u2 = mp[ul];
			int v2 = mp[vl];
			g[u2].emplace_back(v2, w[i]);
			g[v2].emplace_back(u2, w[i]);
		}
	}

	map<int, map<int, long long>>tmp;
	while (q--) {
		int a, b; cin >> a >> b;
		a--, b--;
		long long ans = dp0[a] + dp0[b];
		int a2 = mp[uf.leader(a)];
		int b2 = mp[uf.leader(b)];
		auto dijkstra = [&](int st, int ed)->long long {
			if (tmp.count(st)) {
				if (tmp[st].count(ed))return tmp[st][ed];
				else return LINF * 3;
			}
			map<int, long long>dp;

			using S = std::pair<long long, int>;
			std::priority_queue<S, std::vector<S>, std::greater<S>> pq;
			dp[st] = 0;
			pq.emplace(0, st);
			while (!pq.empty()) {
				auto [d, v] = pq.top();
				pq.pop();

				if (d > dp[v]) continue;

				for (auto [to, cost] : g[v]) {
					long long nd = dp[v] + cost;
					if (dp.count(to) == 0 || nd < dp[to]) {
						dp[to] = nd;
						pq.emplace(nd, to);
					}
				}
			}

			//for (auto [k, v] : dp)cout << k << "?" << v << endl;
			tmp[st] = dp;
			if (tmp[st].count(ed))return tmp[st][ed];
			else return LINF * 3;
		};
		chmin(ans, dijkstra(a2, b2));
		cout << ans << endl;
	}
	return 0;
}
0