// `Be name khoda` //
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxn5 = 1e5 + 10;
const int inf = 1e9;
int ssq = 3;
int h[maxn5], h2[maxn5], dis[2][maxn5][6];
int av[maxn5], av2[maxn5];
vector <pair<int, int>> jda[maxn5], adj[maxn5];
int k;
inline int get_dis(int a, int b){
fill(h + a, h + b + 5, inf);
h[b] = 0;
for(int i = b; i > a; i--){
for(auto [u, t] : jda[i])
h[u] = min(h[u], h[i] + t);
}
return h[a];
}
inline void build(int id){
for(int i = id; i < id + ssq; i++){
fill(h + id, h + id + ssq + 3, inf);
h[i] = 0;
for(int j = i; j < id + ssq; j++) for(auto [u, t] : adj[j])
h[u] = min(h[u], h[j] + t);
for(int j = i; j >= id; j--) for(auto [u, t] : jda[j])
h[u] = min(h[u], h[j] + t);
for(int j = 0; j < k; j++){
dis[0][i][j] = h[id + j];
dis[1][i][j] = h[id + ssq - k + j];
}
}
return;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n, m, q; cin >> k >> n >> m >> q;
ssq *= k;
for(int i = 0; i < m; i++){
int a, b, t; cin >> a >> b >> t;
jda[b].pb({a, t});
adj[a].pb({b, t});
}
for(int i = 0; i < n; i+=ssq)
build(i);
for(int i = 0; i < q; i++){
int a, b; cin >> a >> b;
int cmpa = a / ssq * ssq;
int cmpb = b / ssq * ssq;
if(cmpa == cmpb){
int ans = get_dis(a, b);
if(ans == inf)
ans = -1;
cout << ans << '\n';
continue;
}
for(int i = 0; i < k; i++){ // akhare cmpa
av[i] = cmpa + ssq - k + i;
h[i] = dis[1][a][i];
h2[i] = inf;
}
for(int i = 0; i < k; i++) for(auto [u, t] : adj[av[i]])
h2[u - cmpa - ssq] = min(h2[u - cmpa - ssq], h[i] + t);
for(int i = 0; i < k; i++){ // avale cmpa + ssq
av[i] = cmpa + ssq + i;
h[i] = h2[i];
h2[i] = inf;
}
cmpa += ssq;
while(cmpa != cmpb){
//cout << "hey " << cmpa << ' ' << cmpb << endl;
//for(int i = 0; i < k; i++)
//cout << av[i] << ' ' << h[i] << endl;
for(int i = 0; i < k; i++){ // avale cmpa
av2[i] = cmpa + ssq - k + i;
h2[i] = inf;
}
for(int i = 0; i < k; i++) for(int j = 0; j < k; j++)
h2[j] = min(h2[j], dis[1][av[i]][j] + h[i]);
for(int i = 0; i < k; i++){ // akhare cmpa
av[i] = av2[i];
h[i] = h2[i];
h2[i] = inf;
}
for(int i = 0; i < k; i++) for(auto [u, t] : adj[av[i]])
h2[u - cmpa - ssq] = min(h2[u - cmpa - ssq], h[i] + t);
for(int i = 0; i < k; i++){ // avale cmpa + ssq
av[i] = cmpa + ssq + i;
h[i] = h2[i];
}
cmpa += ssq;
}
int ans = inf;
for(int i = 0; i < k; i++)
ans = min(ans, h[i] + dis[0][b][i]);
if(ans == inf)
ans = -1;
cout << ans << '\n';
}
}
/*
1 11 10 1
0 1 4
1 2 20
2 3 21
3 4 24
4 5 26
5 6 21
6 7 8
7 8 23
8 9 24
9 10 15
0 10
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2039 ms |
11652 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
7 ms |
5076 KB |
Output is correct |
6 |
Correct |
6 ms |
5076 KB |
Output is correct |
7 |
Correct |
7 ms |
5076 KB |
Output is correct |
8 |
Correct |
2071 ms |
11764 KB |
Output is correct |
9 |
Correct |
1953 ms |
11520 KB |
Output is correct |
10 |
Correct |
708 ms |
7716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3006 ms |
11744 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5028 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
5032 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
6 ms |
5372 KB |
Output is correct |
9 |
Correct |
6 ms |
5208 KB |
Output is correct |
10 |
Correct |
65 ms |
11548 KB |
Output is correct |
11 |
Correct |
73 ms |
11668 KB |
Output is correct |
12 |
Correct |
93 ms |
13484 KB |
Output is correct |
13 |
Correct |
109 ms |
14000 KB |
Output is correct |
14 |
Correct |
87 ms |
12776 KB |
Output is correct |
15 |
Correct |
62 ms |
10188 KB |
Output is correct |
16 |
Correct |
73 ms |
10152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5028 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
5032 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
6 ms |
5372 KB |
Output is correct |
9 |
Correct |
6 ms |
5208 KB |
Output is correct |
10 |
Correct |
65 ms |
11548 KB |
Output is correct |
11 |
Correct |
73 ms |
11668 KB |
Output is correct |
12 |
Correct |
93 ms |
13484 KB |
Output is correct |
13 |
Correct |
109 ms |
14000 KB |
Output is correct |
14 |
Correct |
87 ms |
12776 KB |
Output is correct |
15 |
Correct |
62 ms |
10188 KB |
Output is correct |
16 |
Correct |
73 ms |
10152 KB |
Output is correct |
17 |
Correct |
613 ms |
11832 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
5028 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5032 KB |
Output is correct |
23 |
Correct |
12 ms |
5204 KB |
Output is correct |
24 |
Correct |
12 ms |
5156 KB |
Output is correct |
25 |
Correct |
16 ms |
5380 KB |
Output is correct |
26 |
Correct |
13 ms |
5164 KB |
Output is correct |
27 |
Correct |
590 ms |
11548 KB |
Output is correct |
28 |
Correct |
781 ms |
13660 KB |
Output is correct |
29 |
Correct |
827 ms |
14000 KB |
Output is correct |
30 |
Correct |
701 ms |
12912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2039 ms |
11652 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
7 ms |
5076 KB |
Output is correct |
6 |
Correct |
6 ms |
5076 KB |
Output is correct |
7 |
Correct |
7 ms |
5076 KB |
Output is correct |
8 |
Correct |
2071 ms |
11764 KB |
Output is correct |
9 |
Correct |
1953 ms |
11520 KB |
Output is correct |
10 |
Correct |
708 ms |
7716 KB |
Output is correct |
11 |
Execution timed out |
3006 ms |
11744 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |