Submission #155229

#TimeUsernameProblemLanguageResultExecution timeMemory
155229dandrozavrEvacuation plan (IZhO18_plan)C++14
100 / 100
1266 ms29404 KiB
/*
Uruchamiamy samolot zwiadowczy ( + 500% do wzlamaniej )

/▄/  /█/  /◐/   /▐/   /▌/ /▀/ /░/ /🔥/   choose  own style!

***IT'S OUR LONG WAY TO THE OIILLLL***

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░████████░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████░░░░░░░░░░░░░░░░░▌░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░◐◐◐█████████▀▀▀▀▀▀🔥░░░░░░░░███░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████░░░░░░░░░░░░░░░░░░░░▌░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████░░░░░░░░░░░░░░░░░░░░█▌░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▄▄▀██████████████████████████████████████████████████
░░░░░░░░░░░░░░░░░░░░░░░░░░▄▄▄████▄████████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █████
░░░░░░░░░░░░░░░░░░░░░░░░░░░▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█████████▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░◐◐◐█████████▀▀▀▀▀▀🔥░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░████████░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░███████░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████░░░░░░░░░░░░░░░
*/


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
//#pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>
using namespace std;



#define pb push_back
#define ll long long
#define ld long double
#define fi first
#define se second
#define eb emplace_back
#define pii pair < int , int >
#define pipii pair< int, pair < int , int > >
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());

//#include <ext/pb_ds/detail/standard_policies.hpp>'
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;template <typename T> using ordered_set = tree <T, null_type, less< T >, rb_tree_tag,tree_order_statistics_node_update>;
namespace fastio {template <class T> ostream &operator<<(ostream &os, const vector<T> &container){for (auto &u : container)os << u << " ";return os;}template<class T1, class T2> ostream& operator << (ostream& os, const pair<T1, T2>& p) { return os << p.first << " " << p.second; }template <class T> ostream &operator<<(ostream &os, set<T> const& con) { for (auto &u : con) os << u << " "; return os; }void pr() {}template <typename T, typename... args> void pr(T x, args... tail) { cout << x << " "; pr(tail...);}}using namespace fastio;

const int N = 3e5 + 5;
const int MA = 1e6 + 1;

const int X[4] = {0, 0, 1, -1};
const int Y[4] = {-1, 1, 0, 0};

int p[N], sz[N];
vector < pii > g[N];

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

void unite(int x, int y)
{
    x = find_set(x);
    y = find_set(y);
    if (x == y)
        return;
    if (sz[x] > sz[y])
        swap(x, y);
    p[x] = y;
    sz[y] += sz[x];
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #ifdef Estb_probitie
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < m; ++i)
    {
        int x, y, val;
        cin >> x >> y >> val;
        --x, --y;
        g[x].pb({y, val});
        g[y].pb({x, val});
    }

    int k;
    cin >> k;
    priority_queue < pii, vector < pii > , greater < pii >> v;
    int len[n];
    fill(len, len + n, 1e9);

    for (int i = 0; i < k; ++i)
    {
        int x;
        cin >> x;
        --x;
        v.push({0, x});
        len[x] = 0;
    }

    vector < int > all;
    while(v.size())
    {
        pii now = v.top();
        v.pop();
        if (len[now.se] < now.fi) continue;
        all.pb(now.se);
        for (pii to : g[now.se])
            if (to.se + now.fi < len[to.fi])
            {
                len[to.fi] = now.fi + to.se;
                v.push({len[to.fi], to.fi});
            }
    }

    int q;
    cin >> q;
    int x[q], y[q];
    int l[q], r[q];

    for (int i = 0; i < q; ++i)
        cin >> x[i] >> y[i], --x[i], --y[i];
    for (int i = 0; i < q; ++i)
        l[i] = 0, r[i] = n - 1;
    while(1)
    {
        bool ch = 0;
        vector < int > que[n];
        for (int i = 0; i < n; ++i)
            sz[i] = 1, p[i] = i;
        for (int i = 0; i < q; ++i)
        {
            if (l[i] != r[i])
            {
                ll md = ((l[i] + r[i]) >> 1) + 1;

                que[md].pb(i);
                ch = 1;
            }
        }

        if (!ch)
            break;
        for (int i = n - 1; i >= 0; --i)
        {
            for (pii to : g[all[i]])
                if (len[to.fi] >= len[all[i]])
                   unite(to.fi, all[i]);
            for (int cond : que[i])
                {
                    if (find_set(x[cond]) == find_set(y[cond]))
                        l[cond] = i; else
                        r[cond] = i - 1;
                }

        }
        }

    for (int i = 0; i < q; ++i)
        cout << len[all[l[i]]] << '\n';
}
#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...