이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
// SNM
vector <int> par, siz, ans;
vector <set<int>> st;
void make_set(int n) {
par = siz = vector<int> (1+n);
for (int i = 0; i <= n; i++)
par[i] = i, siz[i] = 1;
}
int find_set(int v) {
if (v == par[v]) return v;
return par[v] = find_set(par[v]);
}
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;
vector <pair<int,int>> g[1+n];
vector <int> G[1+n];
vector <int> d(1+n, INT_MAX), used(1+n, 0);
st = vector <set<int>> (1+n);
for (int i = 0; i < m; i++) {
int u, v, cost;
cin >> u >> v >> cost;
g[u].push_back({v, cost}); G[u].push_back(v);
g[v].push_back({u, cost}); G[v].push_back(u);
}
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;
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;
ans = vector <int> (q);
for (int i = 0; i < q; i++) {
int s, t;
cin >> s >> t;
st[s].insert(i);
st[t].insert(i);
}
make_set(n);
vector <int> pos;
for (int i = 1; i <= n; i++) {
pos.push_back(i);
}
sort(pos.begin(), pos.end(), [&](int a, int b) {
return d[a] > d[b];
});
used = vector <int> (1+n, 0);
for (int v: pos) {
used[v] = 1;
for (int to: G[v]) {
if (used[to]) {
uni(v, to);
}
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << '\n';
}*/
int q;
cin >> q;
if ((q <= 200 && n <= 15 && m <= 200)) {
for (int i = 0; i < q; i++) {
int s, t;
cin >> s >> t;
vector <int> d2(1+n, 0); d2[s] = d[s];
priority_queue <pair<int,int>> qw;
qw.push({d2[s], s});
while (!qw.empty()) {
int v = qw.top().second;
qw.pop();
for (int to : G[v]){
if (d2[to] < d2[v]){
d2[to] = min(d[to], d2[v]);
qw.push({d2[to], to});
}
}
}
cout << d2[t] << '\n';
}
} else {
for (int i = 0; i < q; i++) {
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... |