#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});
}
z = 0;
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(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1426 ms |
3908 KB |
Output is correct |
2 |
Correct |
1 ms |
1624 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 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 |
1704 KB |
Output is correct |
8 |
Correct |
1251 ms |
3932 KB |
Output is correct |
9 |
Correct |
1211 ms |
3832 KB |
Output is correct |
10 |
Correct |
538 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3067 ms |
5108 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1372 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 |
1432 KB |
Output is correct |
5 |
Correct |
1 ms |
1880 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1700 KB |
Output is correct |
8 |
Correct |
2 ms |
1628 KB |
Output is correct |
9 |
Correct |
2 ms |
1628 KB |
Output is correct |
10 |
Correct |
26 ms |
3520 KB |
Output is correct |
11 |
Correct |
58 ms |
4444 KB |
Output is correct |
12 |
Correct |
85 ms |
5724 KB |
Output is correct |
13 |
Correct |
92 ms |
6380 KB |
Output is correct |
14 |
Correct |
85 ms |
4944 KB |
Output is correct |
15 |
Correct |
61 ms |
3672 KB |
Output is correct |
16 |
Correct |
56 ms |
3776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1372 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 |
1432 KB |
Output is correct |
5 |
Correct |
1 ms |
1880 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1700 KB |
Output is correct |
8 |
Correct |
2 ms |
1628 KB |
Output is correct |
9 |
Correct |
2 ms |
1628 KB |
Output is correct |
10 |
Correct |
26 ms |
3520 KB |
Output is correct |
11 |
Correct |
58 ms |
4444 KB |
Output is correct |
12 |
Correct |
85 ms |
5724 KB |
Output is correct |
13 |
Correct |
92 ms |
6380 KB |
Output is correct |
14 |
Correct |
85 ms |
4944 KB |
Output is correct |
15 |
Correct |
61 ms |
3672 KB |
Output is correct |
16 |
Correct |
56 ms |
3776 KB |
Output is correct |
17 |
Correct |
1055 ms |
4672 KB |
Output is correct |
18 |
Correct |
1 ms |
1384 KB |
Output is correct |
19 |
Correct |
1 ms |
1628 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
21 |
Correct |
1 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
8 ms |
1788 KB |
Output is correct |
24 |
Correct |
10 ms |
1628 KB |
Output is correct |
25 |
Correct |
22 ms |
1880 KB |
Output is correct |
26 |
Correct |
15 ms |
1628 KB |
Output is correct |
27 |
Correct |
299 ms |
3696 KB |
Output is correct |
28 |
Correct |
1740 ms |
6332 KB |
Output is correct |
29 |
Correct |
1742 ms |
6500 KB |
Output is correct |
30 |
Correct |
1281 ms |
5300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1426 ms |
3908 KB |
Output is correct |
2 |
Correct |
1 ms |
1624 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 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 |
1704 KB |
Output is correct |
8 |
Correct |
1251 ms |
3932 KB |
Output is correct |
9 |
Correct |
1211 ms |
3832 KB |
Output is correct |
10 |
Correct |
538 ms |
2396 KB |
Output is correct |
11 |
Execution timed out |
3067 ms |
5108 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |