이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// 以上帝的名义
// 候选硕士
#include <bits/stdc++.h>
#ifdef local
#include "algo/debug.h"
#else
#define dbg(x...) 0
#endif
using namespace std ;
using ll = long long ;
template<class T> constexpr T inf = ::numeric_limits<T>::max() / 32 * 15 + 208;
const int N = 1e5 + 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> ;
pr[i] = -1 ;
}
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 ;
pr[to] = v ;
st.insert({dist[to], to}) ;
}
}
}
}
vector<int> order ;
ll check(int mid, int s, int t) {
vector<int> is(n + 1, 0) ;
for (int i = 1 ; i <= n ; i++) {
if (dist[i] < mid) 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() ;
for (int i = 1 ; i <= n ; i++) order.push_back(i) ;
sort(order.begin(), order.end(), [&](int u, int v){
return dist[u] > dist[v] ;
}) ;
int q ; cin >> q ;
for (int i = 0 ; i < q ; i++) {
int u , v ; cin >> u >> v ;
int l = 0 , r = n ;
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |