이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
const int maxn = 1e5 + 69;
set<pair<int, int>> g[maxn];
int n, m, k, cnt[maxn], d[maxn], q, mn[maxn];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> m;
for (int i = 1;i <= m;i++) {
int a, b, w;
cin >> a >> b >> w;
g[a].insert({b, w});
g[b].insert({a, w});
}
cin >> k;
for (int i = 1;i <= k;i++) {
int x;
cin >> x;
cnt[x] = 1;
}
cin >> q;
priority_queue<pair<int, int>> pq;
for (int i = 1;i <= n;i++) {
d[i] = 1e18;
if (cnt[i]) {
pq.push({0, i});
d[i] = 0;
}
}
while (!pq.empty()) {
auto v = pq.top().s;
pq.pop();
for (auto u : g[v]) {
if (d[u.f] <= d[v] + u.s)
continue;
d[u.f] = d[v] + u.s;
pq.push({-d[u.f], u.f});
}
}
for (int i = 1;i <= q;i++) {
int s, t;
cin >> s >> t;
auto u = *g[s].lower_bound({t, -1});
if (u.f == t)
cout << min(d[s], d[t]) << '\n';
else {
for (int i = 1;i <= n;i++)
mn[i] = 0;
mn[s] = d[s];
queue<int> q;
q.push(s);
while (!q.empty()) {
auto v = q.front();
q.pop();
for (auto u : g[v]) {
auto vr = min(mn[v], d[u.f]);
if (vr <= mn[u.f])
continue;
mn[u.f] = vr;
q.push(u.f);
}
}
cout << mn[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... |