#include #include 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 template inline bool chmax(A& a, const B& b) { if (a < b) { a = b; return true; } return false; } template 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 #include #include #include #include #include /** * @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 std::pair, std::vector> dijkstra(const std::vector>>& g, int s, T inf) { int n = (int)g.size(); std::vector dist(n, inf); std::vector prev(n, -1); using S = std::pair; std::priority_queue, std::greater> 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 restore_path(const std::vector& prev, int s, int t) { std::vector 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 vectors(n); rep(i, n)cin >> s[i]; long long lim = 2 * LINF; vectoru, v; vectorw; vector g0(n + 1, vector>()); 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); mapmp;//leader,index rep(i, n) { if (uf.leader(i) == i) { mp[i] = mp.size(); } } int sz = mp.size(); vector g(sz, vector < pair>()); { 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>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; } mapdp; using S = std::pair; std::priority_queue, std::greater> 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; }