Submission #414124

#TimeUsernameProblemLanguageResultExecution timeMemory
414124dxz05Evacuation plan (IZhO18_plan)C++14
41 / 100
4096 ms310324 KiB
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << "[" << H << "]";
    debug_out(T...);
}

#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define revall(x) (x).rbegin(), (x).rend()

clock_t startTime;

double getCurrentTime() {
    return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}

#define MP make_pair

typedef long long ll;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.00001;
const int MOD = 998244353;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e5 + 3e2;
const int M = 222;

vector<pair<int, int>> g[N];

int dist[N];
bool processed[N];

int parent[N][715];
int p[N];

int find_set(int x){
    if (p[x] == x) return x;
    return p[x] = find_set(p[x]);
}

void union_sets(int x, int y){
    x = find_set(x);
    y = find_set(y);
    if (x == y) return;
    if (rng() & 1) swap(x, y);
    p[x] = y;
}

void solve(int TC) {
    int n, m;
    cin >> n >> m;

    int SQ = sqrt(m) + 3;

    for (int i = 1; i <= m; i++){
        int u, v, w;
        cin >> u >> v >> w;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }

    for (int i = 1; i <= n; i++){
        dist[i] = INF;
        p[i] = i;
        for (int j = 0; j < 715; j++) parent[i][j] = i;
    }

    priority_queue<pair<int, int>> pq;
    int k;
    cin >> k;

    while (k--){
        int x;
        cin >> x;
        dist[x] = 0;
        pq.push(MP(0, x));
    }

    while (!pq.empty()){
        int x = pq.top().second; pq.pop();
        if (processed[x]) continue;
        processed[x] = true;
        for (auto edge : g[x]){
            int y = edge.first, w = edge.second;
            if (dist[y] > dist[x] + w){
                dist[y] = dist[x] + w;
                pq.push(MP(-dist[y], y));
            }
        }
    }

    vector<pair<int, pair<int, int>>> edges;

    for (int i = 1; i <= n; i++){
        for (auto edge : g[i]){
            int j = edge.first;
            if (i < j){
                edges.emplace_back(min(dist[i], dist[j]), MP(i, j));
            }
        }
    }

    sort(all(edges));

    for (int i = m - 1; i >= 0; i--){
        int x = edges[i].second.first, y = edges[i].second.second;
        union_sets(x, y);
        if (i % SQ == 0){
            for (int j = 1; j <= n; j++){
                parent[j][i / SQ] = find_set(j);
            }
        }
    }

    int q;
    cin >> q;

    while (q--){
        int s, t;
        cin >> s >> t;
        int block = 0;
        for (int bl = 0; bl <= SQ; bl++){
            if (parent[s][bl] == parent[t][bl]) block = bl;
        }

        for (int i = 1; i <= n; i++){
            p[i] = parent[i][block + 1];
        }

        int ans = 0;
        for (int i = min(m, (block + 1) * SQ) - 1; i >= block * SQ; i--){
            int x = edges[i].second.first, y = edges[i].second.second, w = edges[i].first;
            union_sets(x, y);
            if (find_set(s) == find_set(t)){
                ans = w;
                break;
            }
        }

        cout << ans << endl;
    }

}


int main() {
    startTime = clock();
    cin.tie(nullptr); cout.tie(nullptr);
    ios_base::sync_with_stdio(false);

    bool llololcal = false;
#ifdef dddxxz
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    llololcal = true;
#endif

    int TC = 1;
    //cin >> TC;

    for (int test = 1; test <= TC; test++) {
        //debug(test);
        //cout << "Case #" << test << ": ";
        solve(test);
    }

    if (llololcal) cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;

    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...