#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 50001;
vector <pair <int, int>> adj[maxn];
ll dis[maxn];
bool processed[maxn];
queue <int> modified;
int pos[maxn];
vector <vector <ll>> pfx;
int id[maxn];
ll dijkstra(int st, int ed) {
dis[st] = 0;
priority_queue <pair <ll, int>> pq;
pq.push({0, st});
pos[st] = 0;
while (!pq.empty()) {
int s = pq.top().second;
pq.pop();
if (processed[s])
continue;
processed[s] = true;
modified.push(s);
id[s] = (int)pfx.size()-1;
pfx.back().push_back(dis[s]);
for (auto i : adj[s]) {
if (dis[i.first] > dis[s] + i.second) {
dis[i.first] = dis[s] + i.second;
pq.push({-dis[i.first], i.first});
pos[i.first] = pos[s] + 1;
}
}
}
ll ans = 0;
if (dis[ed] == 1e18) {
dis[ed] = -1;
modified.push(ed);
}
ans = dis[ed];
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int K, N, M, O;
cin >> K >> N >> M >> O;
for (int i = 0; i < M; i++) {
int a, b, t;
cin >> a >> b >> t;
adj[a].push_back({b, t});
}
fill(dis, dis + N + 1, 1e18);
pfx.emplace_back();
for (int i = 0; i < N; i++) {
if (!adj[i].empty() && !processed[i]) {
pfx.emplace_back();
dijkstra(i, i);
}
}
while (O--) {
int a, b;
cin >> a >> b;
if (id[a] != id[b] || min(id[a], id[b]) == 0)
cout << -1 << '\n';
else
cout << pfx[id[a]][pos[b]] - pfx[id[a]][pos[a]] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
4640 KB |
Output is correct |
2 |
Correct |
1 ms |
1652 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1648 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1664 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
14 ms |
5704 KB |
Output is correct |
9 |
Correct |
15 ms |
5468 KB |
Output is correct |
10 |
Correct |
3 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
4608 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1628 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1628 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
4640 KB |
Output is correct |
2 |
Correct |
1 ms |
1652 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1648 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1664 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
14 ms |
5704 KB |
Output is correct |
9 |
Correct |
15 ms |
5468 KB |
Output is correct |
10 |
Correct |
3 ms |
2140 KB |
Output is correct |
11 |
Incorrect |
21 ms |
4608 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |