Submission #893031

#TimeUsernameProblemLanguageResultExecution timeMemory
893031vjudge1Evacuation plan (IZhO18_plan)C++17
41 / 100
4075 ms68808 KiB
// 以上帝的名义
// 候选硕士
#include <bits/stdc++.h>

#ifdef local
#include "algo/debug.h"
#else
#define dbg(x...) 0
#endif

using namespace std ;
using ll = long long ;

#define int ll

template<class T> constexpr T inf = ::numeric_limits<T>::max() / 32 * 15 + 208;

const int N = 1e6 + 5 ;

int n , m , k , city[N] ;
ll dist[N], pr[N] ;
vector<pair<ll,int>> g[N] ;

void djickstra() {
    for (int i = 1 ; i <= n ; i++) {
        dist[i] = inf<ll> ;
    }
    set<pair<ll,int>> st;
    for (int i = 0 ; i < k ; i++) {
        st.insert({0, city[i]}) ;
        dist[city[i]] = 0 ;
    }
    while (st.size()) {
        auto [val, v] = *st.begin() ;
        st.erase(st.begin()) ;
        for (auto [w, to] : g[v]) {
            if (dist[to] > dist[v] + w) {
                st.erase({dist[to], to}) ;
                dist[to] = dist[v] + w ;
                st.insert({dist[to], to}) ;
            }
        }
    }
}

ll check(int x, int s, int t) {
    vector<int> is(n + 1, 0) ;
    for (int i = 1 ; i <= n ; i++) {
        if (dist[i] < x) continue ;
        is[i] = 1 ;
    }
    if (!is[s] || !is[t]) return 0 ;
    vector<int> use(n + 1, 0) ;
    queue<int> q ;
    q.push(s) ;
    pr[s] = -1 ;
    use[s] = 1 ;
    while (q.size()) {
        int v = q.front() ;
        q.pop() ;
        for (auto [w, to] : g[v]) {
            if (!is[to] || use[to]) continue ;
            pr[to] = v ;
            q.push(to) ;
            use[to] = 1 ;
        }
    }
    if (!use[t]) {
        return 0 ;
    }
    return 1 ;
}

int32_t main() {
    cin.tie(0)->sync_with_stdio(false) ;
    cin >> n >> m ;
    for (int i = 0 ; i < m ; i++) {
        ll u , v , w ; cin >> u >> v >> w ;
        g[u].push_back({w, v}) ;
        g[v].push_back({w, u}) ;
    }
    cin >> k ;
    for (int i = 0 ; i < k ; i++) {
        cin >> city[i] ;
    }
    djickstra() ;
    int q ; cin >> q ;
    for (int i = 0 ; i < q ; i++) {
        int u , v ; cin >> u >> v ;
        ll l = 0 , r = 1e9 ;
        while (l < r) {
            ll md = (1 + l + r) / 2 ;
            if (check(md, u, v)) l = md ;
            else r = md - 1 ;
        }
        cout << l << "\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...