#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int N = 5e4;
const int mod = 1e17;
vector< pair<int, int> > g[N];
int k, n, m, q;
main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> k >> n >> m >> q;
vector<int> dis(n, mod), parent(n, -1);
for(int i = 0;i < m; i++){
int a, b, w; cin >> a >> b >> w;
g[a].push_back({b, w});
}
vector<pair<int, int> > Q;
int z = 0;
for(int i = 0;i < q; i++){
int a, b; cin >> a >> b;
z+= (a == 0);
Q.push_back({a, b});
}
dis[0] = 0;
for(int i = 0; i < n; i++){
for(auto [to, w] : g[i]){
if(dis[to] > dis[i] + w){
dis[to] = dis[i] + w;
parent[to] = i;
}
}
}
if(z == q){
for(auto [a, b] : Q){
if(a == b){
cout << 0 << '\n';
continue;
}
if(dis[b] >= mod or dis[a] >= mod) cout << "-1" << '\n';
else cout << dis[b] - dis[parent[a]]<< '\n';
}
return 0;
}
for(auto [a, b] : Q){
if(a == b){
cout << 0 << '\n';
continue;
}
for(int i = 0;i < n; i++) dis[i] = mod;
dis[a] = 0;
for(int i = a; i < n; i++){
for(auto [to, w] : g[i]){
if(dis[to] > dis[i] + w){
dis[to] = dis[i] + w;
}
}
}
if(dis[b] >= mod) cout << "-1\n";
else cout << dis[b] << '\n';
}
return 0;
}
Compilation message
toll.cpp:12:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
12 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1380 ms |
4356 KB |
Output is correct |
2 |
Correct |
1 ms |
1368 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
3 ms |
1628 KB |
Output is correct |
6 |
Correct |
3 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1628 KB |
Output is correct |
8 |
Correct |
1314 ms |
4188 KB |
Output is correct |
9 |
Correct |
1263 ms |
4224 KB |
Output is correct |
10 |
Correct |
518 ms |
2720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
5212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
2 ms |
1628 KB |
Output is correct |
9 |
Correct |
2 ms |
1628 KB |
Output is correct |
10 |
Correct |
24 ms |
3932 KB |
Output is correct |
11 |
Correct |
61 ms |
5208 KB |
Output is correct |
12 |
Correct |
96 ms |
6580 KB |
Output is correct |
13 |
Correct |
103 ms |
6744 KB |
Output is correct |
14 |
Correct |
91 ms |
5464 KB |
Output is correct |
15 |
Correct |
60 ms |
3928 KB |
Output is correct |
16 |
Correct |
60 ms |
3932 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 |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
2 ms |
1628 KB |
Output is correct |
9 |
Correct |
2 ms |
1628 KB |
Output is correct |
10 |
Correct |
24 ms |
3932 KB |
Output is correct |
11 |
Correct |
61 ms |
5208 KB |
Output is correct |
12 |
Correct |
96 ms |
6580 KB |
Output is correct |
13 |
Correct |
103 ms |
6744 KB |
Output is correct |
14 |
Correct |
91 ms |
5464 KB |
Output is correct |
15 |
Correct |
60 ms |
3928 KB |
Output is correct |
16 |
Correct |
60 ms |
3932 KB |
Output is correct |
17 |
Correct |
1126 ms |
5072 KB |
Output is correct |
18 |
Correct |
1 ms |
1368 KB |
Output is correct |
19 |
Correct |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
1 ms |
1444 KB |
Output is correct |
21 |
Correct |
1 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1624 KB |
Output is correct |
23 |
Correct |
7 ms |
1784 KB |
Output is correct |
24 |
Correct |
11 ms |
1820 KB |
Output is correct |
25 |
Correct |
19 ms |
1884 KB |
Output is correct |
26 |
Correct |
16 ms |
1624 KB |
Output is correct |
27 |
Correct |
342 ms |
3932 KB |
Output is correct |
28 |
Correct |
1780 ms |
6476 KB |
Output is correct |
29 |
Correct |
1653 ms |
7140 KB |
Output is correct |
30 |
Correct |
1236 ms |
5692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1380 ms |
4356 KB |
Output is correct |
2 |
Correct |
1 ms |
1368 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
3 ms |
1628 KB |
Output is correct |
6 |
Correct |
3 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1628 KB |
Output is correct |
8 |
Correct |
1314 ms |
4188 KB |
Output is correct |
9 |
Correct |
1263 ms |
4224 KB |
Output is correct |
10 |
Correct |
518 ms |
2720 KB |
Output is correct |
11 |
Incorrect |
25 ms |
5212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |