#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 = 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(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1225 ms |
4560 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 |
3 ms |
1628 KB |
Output is correct |
6 |
Correct |
3 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
1083 ms |
4796 KB |
Output is correct |
9 |
Correct |
1111 ms |
4452 KB |
Output is correct |
10 |
Correct |
402 ms |
2448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
6180 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 |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1632 KB |
Output is correct |
5 |
Correct |
1 ms |
1632 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
3 ms |
1628 KB |
Output is correct |
9 |
Correct |
2 ms |
1628 KB |
Output is correct |
10 |
Correct |
23 ms |
3932 KB |
Output is correct |
11 |
Correct |
62 ms |
5720 KB |
Output is correct |
12 |
Correct |
90 ms |
7248 KB |
Output is correct |
13 |
Correct |
97 ms |
8020 KB |
Output is correct |
14 |
Correct |
86 ms |
6228 KB |
Output is correct |
15 |
Correct |
66 ms |
4956 KB |
Output is correct |
16 |
Correct |
61 ms |
4696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1632 KB |
Output is correct |
5 |
Correct |
1 ms |
1632 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
3 ms |
1628 KB |
Output is correct |
9 |
Correct |
2 ms |
1628 KB |
Output is correct |
10 |
Correct |
23 ms |
3932 KB |
Output is correct |
11 |
Correct |
62 ms |
5720 KB |
Output is correct |
12 |
Correct |
90 ms |
7248 KB |
Output is correct |
13 |
Correct |
97 ms |
8020 KB |
Output is correct |
14 |
Correct |
86 ms |
6228 KB |
Output is correct |
15 |
Correct |
66 ms |
4956 KB |
Output is correct |
16 |
Correct |
61 ms |
4696 KB |
Output is correct |
17 |
Correct |
1082 ms |
6124 KB |
Output is correct |
18 |
Correct |
1 ms |
1628 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 |
1824 KB |
Output is correct |
22 |
Correct |
1 ms |
1632 KB |
Output is correct |
23 |
Correct |
6 ms |
1880 KB |
Output is correct |
24 |
Correct |
12 ms |
1628 KB |
Output is correct |
25 |
Correct |
20 ms |
1952 KB |
Output is correct |
26 |
Correct |
16 ms |
1884 KB |
Output is correct |
27 |
Correct |
330 ms |
4292 KB |
Output is correct |
28 |
Correct |
1922 ms |
8020 KB |
Output is correct |
29 |
Correct |
1846 ms |
8292 KB |
Output is correct |
30 |
Correct |
1262 ms |
6564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1225 ms |
4560 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 |
3 ms |
1628 KB |
Output is correct |
6 |
Correct |
3 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
1083 ms |
4796 KB |
Output is correct |
9 |
Correct |
1111 ms |
4452 KB |
Output is correct |
10 |
Correct |
402 ms |
2448 KB |
Output is correct |
11 |
Incorrect |
24 ms |
6180 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |