이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <assert.h>
#include <vector>
#include <queue>
using namespace std;
#define ll long long
#define sz(x) (int)x.size()
#define pii pair < int, int >
#define METH ios::sync_with_stdio(0); cin.tie(0);
#define BEGIN cout << "BEGIN" << endl;
#define END cout << "END" << endl;
const int N = 1e5;
const int mod = 1e9 + 7; /// ANOTHER HASH MOD: 228228227
const int prime = 29; /// ANOTHER HASH PRIME: 997
const int INF = 1e9 + 7;
int n, m, k, q;
vector < int > dist(N + 9, INF);
vector < pii > g[N + 9];
inline void purify() {
}
inline void precalc() {
}
void check(int a, int b) {
bool ok = false;
for (pii i : g[a]) {
if (i.first == b) {
ok = true;
}
}
assert(ok);
}
inline void read() {
int a, b, c;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
}
void dij() {
priority_queue < pii > pq;
cin >> k;
int a;
for (int i = 1; i <= k; i++) {
cin >> a;
pq.push({0, a});
dist[a] = 0;
}
while (pq.size()) {
int cur = pq.top().second;
int d = -pq.top().first;
pq.pop();
if (d > dist[cur]) {
continue;
}
for (pii i : g[cur]) {
int u = i.first;
int len = i.second;
if (dist[cur] + len < dist[u]) {
dist[u] = dist[cur] + len;
pq.push({-dist[u], u});
}
}
}
}
inline void solve() {
dij();
cin >> q;
for (int i = 1; i <= q; i++) {
int a, b;
cin >> a >> b;
check(a, b);
int ans = dist[b];
if (dist[a] < dist[b]) {
ans = dist[a];
}
cout << ans << endl;
}
}
int main() {
int t;
//scanf("%d", &t);
t = 1;
//precalc();
while (t--) {
//purify();
read();
solve();
}
}
# | 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... |