제출 #344732

#제출 시각아이디문제언어결과실행 시간메모리
344732SprdaloEvacuation plan (IZhO18_plan)C++17
100 / 100
653 ms53972 KiB
#include <bits/stdc++.h>

using namespace std;

#define int ll
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;


const int maxn = 1e5+5;
vp e[maxn], t;
vi d(maxn), g[maxn], sol(maxn);
template<int size> struct dsu{

    int p[size + 10];

    void init(){
        for (int i = 0; i < size + 10; ++i){
            p[i] = i;
        }
    }

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

    void make(int x){
        p[x] = x;
    }

    void spoj(int a, int b){
        int dist = min(d[a], d[b]);
        a = find(a);
        b = find(b);

        if (a != b){
            if (g[a].size() > g[b].size())
                swap(a, b);

            p[a] = b;
            for (int i : g[a]){
                g[b].push_back(i);
                if (find(t[i].first) == find(t[i].second))
                    sol[i] = max(sol[i], dist);
            }
        }
    }
};
dsu<131072> ds;

struct st{
    int u;
    int v;

    bool operator<(const st& other)const{
        return min(d[u], d[v])>min(d[other.u], d[other.v]);
    }
};

vector<st> h;

signed main()
{
    ios_base::sync_with_stdio(false); 
    cin.tie(nullptr); 
    cout.tie(nullptr); 
    cerr.tie(nullptr);    

    int n, m;
    cin >> n >> m;
 
    for (int i = 0; i < m; ++i){
        int u, v, w;
        cin >> u >> v >> w;
 
        e[u].push_back({v, w});
        e[v].push_back({u, w});
        h.push_back({u, v});
    }
 
    int k;
    cin >> k;
 
    queue<pi> q;
    d = vi(n+1,INT64_MAX/10);
    for (int i = 0; i < k; ++i){
        int x;
        cin >> x;
        d[x] = 0;
        q.push({0, x});
    }
 
    while(!q.empty()){
        int c = q.front().second, dist = q.front().first;
        q.pop();
 
        if (d[c] < dist) continue;
 
 
        for (pi p : e[c]){
            if (d[p.first] > p.second + dist){
                d[p.first] = p.second+dist;
                q.push({d[p.first], p.first});
            }
        }
    }

    int Q;
    cin >> Q;

    for (int i = 0; i < Q; ++i){
        int u, v;
        cin >> u >> v;

        g[u].push_back(i);
        g[v].push_back(i);
        t.push_back({u, v});
    }

    sort(h.begin(), h.end());

    ds.init();

    for (int i = 0; i < m; ++i){
        int u = h[i].u, v = h[i].v;

        ds.spoj(u, v);
    }

    for (int i = 0; i < Q; ++i)
        cout << sol[i] << '\n';
}
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5
*/
#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...