Submission #1327252

#TimeUsernameProblemLanguageResultExecution timeMemory
1327252kawhietEvacuation plan (IZhO18_plan)C++20
41 / 100
4093 ms26400 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...) 47
#endif

struct DSU {
    vector<int> p;
    // vector<set<int>> a;

    DSU(int n) {
        p.assign(n + 1, -1);
        // a.resize(n + 1);
        // for (int i = 1; i <= n; i++) {
        //     a[i].insert(i);
        // }
    }

    int root(int x) { return (p[x] == -1 ? x : (p[x] = root(p[x]))); }

    bool link(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        p[y] = x;
        // if (a[x].size() < a[y].size()) {
        //     swap(a[x], a[y]);
        // }
        // for (auto k : a[y]) {
        //     a[x].insert(k);
        // }
        // a[y].clear();
        return true;
    }
};

constexpr int N = 1e5 + 1;
constexpr int inf = 1e9;

int dist[N];
vector<array<int, 2>> g[N];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<array<int, 2>> e;
    for (int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        e.push_back({u, v});
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    fill(dist, dist + N, inf);
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
        int u;
        cin >> u;
        dist[u] = 0;
        q.emplace(0, u);
    }
    while (!q.empty()) {
        auto [s, u] = q.top();
        q.pop();
        if (dist[u] < s) continue;
        for (auto [v, w] : g[u]) {
            if (s + w < dist[v]) {
                dist[v] = s + w;
                q.emplace(s + w, v);
            }
        }
    }
    int t;
    cin >> t;
    vector<vector<array<int, 2>>> h(n + 1);
    set<array<int, 3>> qs;
    // vector<array<int, 3>> qs(t);
    for (int i = 0; i < t; i++) {
        int l, r;
        cin >> l >> r;
        h[l].push_back({r, i});
        h[r].push_back({l, i});
        qs.insert({l, r, i});
        // qs[i] = {l, r, i};
    }
    vector<array<int, 2>> x(n + 1);
    for (int i = 1; i <= n; i++) {
        x[i] = {dist[i], i};
    }
    ranges::sort(x);
    DSU dsu(n + 1);
    vector<int> ans(t);
    vector<bool> allowed(n + 1);
    for (int ptr = n; ptr >= 1; ptr--) {
        auto [d, u] = x[ptr];
        allowed[u] = 1;
        bool ok = 0;
        for (auto [v, _] : g[u]) {
            if (allowed[v]) {
                ok = 1;
                dsu.link(u, v);
            }
        }
        if (!ok) continue;
        // dbg(u);
        // int x = dsu.root(u);
        // for (auto [v, pos] : h[u]) {
        //     if (dsu.root(v) == x && ans[pos] == 0) {
        //         ans[pos] = d;
        //     }
        // }
        vector<array<int, 3>> rem;
        for (auto [i, j, idx] : qs) {
            if (dsu.root(i) == dsu.root(j) && ans[idx] == 0) {
                rem.push_back({i, j, idx});
                ans[idx] = d;
            }
        }
        for (auto p : rem) {
            qs.erase(p);
        }
    }
    for (int i = 0; i < t; i++) {
        cout << ans[i] << '\n';
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...