This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector <int> par, siz;
int find_set(int v) {
if (v == par[v]) return v;
return par[v] = find_set(par[v]);
}
vector <int> d, used;
vector <pair<int,int>> g[100001];
int dfs(int s, int t) {
if (s == t)
return d[t];
used[s] = 1;
vector <pair<int,int>> srt;
for (pair<int,int> i: g[s]) {
int to = i.first;
if (used[to]) continue;
srt.push_back({d[s],to});
}
sort(srt.begin(), srt.end(),[](pair<int,int> a, pair<int,int> b) {
return a.first > b.first;
});
int ans = -2e9;
for (pair<int,int> i: srt) {
ans = max(ans, dfs(i.second, t));
}
return min(d[s], ans);
}
void uni(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (siz[a] < siz[b])
swap(a, b);
siz[a] += siz[b];
par[b] = a;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v, cost;
cin >> u >> v >> cost;
g[u].push_back({v, cost});
g[v].push_back({u, cost});
}
int k;
cin >> k;
vector <int> r(k);
for (int i = 0; i < k; i++) {
cin >> r[i];
}
priority_queue <pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> qw;
used = vector <int> (1 + n, 0);
d = vector <int> (1 + n, INT_MAX);
for (int i = 0; i < k; i++) {
qw.push({0, r[i]});
d[r[i]] = 0;
}
while (!qw.empty()) {
int v = qw.top().second;
qw.pop();
if (used[v]) continue;
used[v] = 1;
for (pair<int,int> i : g[v]) {
int to = i.first, len = i.second;
if (d[v] + len < d[to]) {
d[to] = d[v] + len;
qw.push({d[to], to});
}
}
}
int q;
cin >> q;
if (n <= 15 && m <= 200 && q <= 200) {
for (int tmp = q; tmp--;) {
int s, t;
cin >> s >> t;
used = vector <int> (1 + n, 0);
cout << dfs(s, t) << '\n';
}
return 0;
}
for (int tmp = q; tmp--;) {
int s, t;
cin >> s >> t;
cout << min(d[s], d[t]) << '\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... |