제출 #470470

#제출 시각아이디문제언어결과실행 시간메모리
470470Killer2501Evacuation plan (IZhO18_plan)C++14
100 / 100
1000 ms112860 KiB
#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<ll, pll>
#define pb push_back
#define fi first
#define se second
using namespace std;
const int N = 5e5 + 5;
const int M = 1300;
const ll mod = 1e15 + 7;
ll n, m, t, k, ans, b[N], dp[N], a[N], d[N], l[N], r[N], lab[N];
vector<pll> adj[N];
vector<ll> kq, adt[N];
vector<pii> e;
string s[N];
ll pw(ll k, ll n)
{
    ll total = 1;
    for(; n; n >>= 1)
    {
        if(n & 1)total = total * k % mod;
        k = k * k % mod;
    }
    return total;
}
ll findp(ll u)
{
    return lab[u] < 0 ? u : lab[u] = findp(lab[u]);
}
void hop(ll u, ll v)
{
    if(lab[u] > lab[v])swap(u, v);
    lab[u] += lab[v];
    lab[v] = u;
}
void sol()
{
    cin >> n >> m;
    for(int i = 0; i < m; i ++)
    {
        ll x, y, w;
        cin >> x >> y >> w;
        e.pb({w, {x, y}});
        adj[x].pb({w, y});
        adj[y].pb({w, x});
    }
    fill_n(d, n+1, mod);
    cin >> k;
    priority_queue< pll, vector<pll>, greater<pll> > pq;
    while(k -- > 0)
    {
        cin >> t;
        d[t] = 0;
        pq.push({0, t});
    }
    while(!pq.empty())
    {
        pll u = pq.top();
        pq.pop();
        if(u.fi != d[u.se])continue;
        for(pll v: adj[u.se])
        {
            if(d[v.se] > u.fi + v.fi)
            {
                d[v.se] = u.fi + v.fi;
                pq.push({d[v.se], v.se});
            }
        }
    }
    for(int i = 0; i < m; i ++)e[i].fi = min(d[e[i].se.fi], d[e[i].se.se]);
    sort(e.begin(), e.end());
    cin >> t;
    for(int i = 1; i <= t; i ++)
    {
        cin >> a[i] >> b[i];
        l[i] = 0, r[i] = m-1;
    }
    bool ok = true;
    while(ok)
    {
        ok = false;
        for(int i = 1; i <= t; i ++)
        {
            if(l[i] <= r[i])
            {
                adt[(l[i]+r[i])/2].pb(i);
                ok = true;
            }
        }
        fill_n(lab, n+1, -1);
        for(int i = m-1; i >= 0; i --)
        {
            ll u = findp(e[i].se.fi), v = findp(e[i].se.se);
            if(u != v)hop(u, v);
            for(ll j: adt[i])
            {
                u = findp(a[j]), v = findp(b[j]);
                if(u == v)l[j] = i+1;
                else r[j] = i-1;
            }
            adt[i].clear();
        }
    }
    for(int i = 1; i <= t; i ++)cout << e[r[i]].fi << '\n';

}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    int test = 1;
    //cin >> test;
    while (test-- > 0)
        sol();
    return 0;
}
/*
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
*/
// ctrl + F8 = compile
// ctrl + F9 = run
#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...