이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 1e5 + 1;
int par[N];
set<int> lst[N];
int ans[N];
vector<pair<int, int>> g[N];
int get_root(int v) {
return v == par[v] ? v : par[v] = get_root(par[v]);
}
void un(int v, int u, int kek) {
v = get_root(v);
u = get_root(u);
if (lst[v].size() < lst[u].size()) {
swap(v, u);
}
if (v != u) {
par[u] = v;
for (int x : lst[u]) {
if (lst[v].count(x)) {
ans[x] = kek;
lst[v].erase(x);
} else {
lst[v].insert(x);
}
}
}
}
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
iota(par, par + n + 1, 0);
for (int i = 0; i < m; i++) {
int v, u, w;
cin >> v >> u >> w;
g[v].emplace_back(u, w);
g[u].emplace_back(v, w);
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int v;
cin >> v;
g[0].emplace_back(v, 0);
}
vector<int> dist(n + 1, INT32_MAX);
dist[0] = 0;
set<pair<int, int>> st;
st.insert({ 0, 0 });
while (!st.empty()) {
auto p = *st.begin();
st.erase(st.begin());
int v = p.second;
for (auto e : g[v]) {
if (dist[v] + e.second < dist[e.first]) {
st.erase({ dist[e.first], e.first });
dist[e.first] = dist[v] + e.second;
st.insert({ dist[e.first], e.first });
}
}
}
int Q;
cin >> Q;
for (int i = 0; i < Q; i++) {
int s, t;
cin >> s >> t;
if (s != t) {
lst[s].insert(i);
lst[t].insert(i);
} else {
ans[i] = dist[s];
}
}
vector<int> p(n);
iota(p.begin(), p.end(), 1);
sort(p.begin(), p.end(), [&](int a, int b) {
return dist[a] > dist[b];
});
vector<int> used(n + 1);
for (int i = 0; i < n; i++) {
int v = p[i];
used[v] = 1;
for (auto e : g[v]) {
if (used[e.first]) {
un(v, e.first, dist[v]);
}
}
}
for (int i = 0; i < Q; i++) {
cout << ans[i] << '\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... |