#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;
ll dijkstra(int st, int ed) {
dis[st] = 0;
priority_queue <pair <ll, int>> pq;
pq.push({0, st});
while (!pq.empty()) {
int s = pq.top().second;
pq.pop();
if (processed[s])
continue;
processed[s] = true;
modified.push(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});
}
}
}
int ans = 0;
if (ed != -1) {
if (dis[ed] == 1e18) {
dis[ed] = -1;
modified.push(ed);
}
ans = dis[ed];
while (!modified.empty()) {
dis[modified.front()] = 1e18;
processed[modified.front()] = false;
modified.pop();
}
}
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);
vector <pair <int, int>> queries(O);
bool all0 = true;
for (int i = 0; i < O; i++) {
cin >> queries[i].first >> queries[i].second;
if (queries[i].first != 0)
all0 = false;
}
if (all0)
dijkstra(0, -1);
for (int i = 0; i < O; i++) {
if (all0) {
if (dis[queries[i].second] == 1e18)
cout << -1 << '\n';
else
cout << dis[queries[i].second] << '\n';
} else {
cout << dijkstra(queries[i].first, queries[i].second) << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3066 ms |
3924 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
3676 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
3 ms |
1628 KB |
Output is correct |
9 |
Correct |
12 ms |
3532 KB |
Output is correct |
10 |
Correct |
32 ms |
4688 KB |
Output is correct |
11 |
Correct |
23 ms |
3920 KB |
Output is correct |
12 |
Correct |
16 ms |
3420 KB |
Output is correct |
13 |
Correct |
39 ms |
4620 KB |
Output is correct |
14 |
Correct |
21 ms |
3676 KB |
Output is correct |
15 |
Correct |
18 ms |
3344 KB |
Output is correct |
16 |
Correct |
18 ms |
3160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1628 KB |
Output is correct |
8 |
Correct |
8 ms |
1628 KB |
Output is correct |
9 |
Correct |
5 ms |
1628 KB |
Output is correct |
10 |
Correct |
15 ms |
3420 KB |
Output is correct |
11 |
Correct |
191 ms |
3760 KB |
Output is correct |
12 |
Correct |
256 ms |
4700 KB |
Output is correct |
13 |
Correct |
292 ms |
4868 KB |
Output is correct |
14 |
Correct |
257 ms |
4328 KB |
Output is correct |
15 |
Correct |
172 ms |
3164 KB |
Output is correct |
16 |
Correct |
168 ms |
3300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1628 KB |
Output is correct |
8 |
Correct |
8 ms |
1628 KB |
Output is correct |
9 |
Correct |
5 ms |
1628 KB |
Output is correct |
10 |
Correct |
15 ms |
3420 KB |
Output is correct |
11 |
Correct |
191 ms |
3760 KB |
Output is correct |
12 |
Correct |
256 ms |
4700 KB |
Output is correct |
13 |
Correct |
292 ms |
4868 KB |
Output is correct |
14 |
Correct |
257 ms |
4328 KB |
Output is correct |
15 |
Correct |
172 ms |
3164 KB |
Output is correct |
16 |
Correct |
168 ms |
3300 KB |
Output is correct |
17 |
Execution timed out |
3037 ms |
3924 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3066 ms |
3924 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |