1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include <bits/stdc++.h> using LL = long long; const LL INF = 1e18; const int N = 2e5 + 100; int n, m, k, tq, fa[N], h[N], idx = 0, ct = 0; struct Edge{ int ne, ver, w; } e[N << 1]; struct Edge2{ int u, v; LL w; } e2[N]; struct Q{ int x, y, id; LL t; } que[N]; LL dis[N]; bool vis[N], ans[N]; int get(int x){ return x == fa[x] ? x : fa[x] = get(fa[x]); } void add(int x, int y, int z){ e[idx] = {h[x], y, z}, h[x] = idx++; } void dij() { memset(vis, false, sizeof vis); std::priority_queue< std::pair<LL, int> > q; for (int i = k + 1; i <= n; ++i) dis[i] = INF; for (int i = 1; i <= k; ++i) q.push({0, i}); while (!q.empty()) { int x = q.top().second; q.pop(); if (vis[x]) continue; vis[x] = true; for (int i = h[x], y; ~i; i = e[i].ne) if (!vis[y = e[i].ver]) if (dis[y] > dis[x] + e[i].w) { dis[y] = dis[x] + e[i].w; q.push({-dis[y], y}); } } } void merge(int x, int y){ fa[get(x)] = get(y); } int main() { memset(h, -1, sizeof h); scanf("%d %d %d", &n, &m, &k); for (int i = 1, u, v, w; i <= m; ++i) { scanf("%d %d %d", &u, &v, &w); add(u, v, w), add(v, u, w); } dij(); for (int i = 0; i < idx; i += 2) { int u = e[i].ver, v = e[i ^ 1].ver, w = e[i].w; e2[++ct] = {u, v, dis[u] + dis[v] + w}; } std::sort(e2 + 1, e2 + ct + 1, [&](Edge2 a, Edge2 b){ return a.w < b.w; }); scanf("%d", &tq); for (int i = 1; i <= tq; ++i) { scanf("%d %d %lld", &que[i].x, &que[i].y, &que[i].t); que[i].id = i; } std::sort(que + 1, que + tq + 1, [&](Q a, Q b){ return a.t < b.t; }); for (int i = 1; i <= n; ++i) fa[i] = i; for (int i = 1, j = 1; i <= tq; ++i) { for (; j <= ct && e2[j].w <= que[i].t; ++j) merge(e2[j].u, e2[j].v); ans[que[i].id] = get(que[i].x) == get(que[i].y); } for (int i = 1; i <= tq; ++i) if (ans[i]) puts("Yes"); else puts("No"); return 0; }
|