#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);
for(int i = 0;i < m; i++){
int a, b, w; cin >> a >> b >> w;
dis[b] = min(dis[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});
}
if(z == q){
for(auto [a, b] : Q){
if(a == b){
cout << 0 << '\n';
continue;
}
if(dis[b] >= mod or a != 0) cout << "-1\n";
else cout << dis[b] << '\n';
}
return 0;
}
for(auto [a, b] : Q){
if(a == b){
cout << 0 << '\n';
continue;
}
for(int i = 1;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(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1273 ms |
4052 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
2 ms |
1624 KB |
Output is correct |
6 |
Runtime error |
4 ms |
3088 KB |
Execution killed with signal 6 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
4828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 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 |
1372 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 |
25 ms |
3576 KB |
Output is correct |
11 |
Correct |
60 ms |
4444 KB |
Output is correct |
12 |
Correct |
90 ms |
5712 KB |
Output is correct |
13 |
Correct |
98 ms |
6224 KB |
Output is correct |
14 |
Correct |
86 ms |
5208 KB |
Output is correct |
15 |
Correct |
65 ms |
3676 KB |
Output is correct |
16 |
Correct |
61 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 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 |
1372 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 |
25 ms |
3576 KB |
Output is correct |
11 |
Correct |
60 ms |
4444 KB |
Output is correct |
12 |
Correct |
90 ms |
5712 KB |
Output is correct |
13 |
Correct |
98 ms |
6224 KB |
Output is correct |
14 |
Correct |
86 ms |
5208 KB |
Output is correct |
15 |
Correct |
65 ms |
3676 KB |
Output is correct |
16 |
Correct |
61 ms |
3676 KB |
Output is correct |
17 |
Correct |
1084 ms |
4664 KB |
Output is correct |
18 |
Correct |
1 ms |
1624 KB |
Output is correct |
19 |
Correct |
1 ms |
1628 KB |
Output is correct |
20 |
Correct |
1 ms |
1624 KB |
Output is correct |
21 |
Correct |
1 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
6 ms |
1800 KB |
Output is correct |
24 |
Correct |
12 ms |
1884 KB |
Output is correct |
25 |
Correct |
20 ms |
1884 KB |
Output is correct |
26 |
Correct |
16 ms |
1692 KB |
Output is correct |
27 |
Correct |
344 ms |
3676 KB |
Output is correct |
28 |
Correct |
1924 ms |
6064 KB |
Output is correct |
29 |
Correct |
1895 ms |
6752 KB |
Output is correct |
30 |
Correct |
1288 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1273 ms |
4052 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
2 ms |
1624 KB |
Output is correct |
6 |
Runtime error |
4 ms |
3088 KB |
Execution killed with signal 6 |
7 |
Halted |
0 ms |
0 KB |
- |