#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;
const ll inf = 1e18;
const int i_inf = 1e9;
const ll mod = 1e9+7;
mt19937 _rand(time(NULL));
const int mxn = 2e5;
int n, k, m, q;
vector<pii> g[mxn];
ll sparse[mxn][17][5][5];
ll aux[5][5];
void fillaux(int u, int p){
fr(i, 0, k){
fr(j, 0, k){
aux[i][j] = inf;
}
}
fr(i, 0, k){
fr(j, 0, k){
ll w = sparse[u][p][i][j];
int pos = (u+(1<<p)-1)*k + j;
for(auto v : g[pos]){
int jp = v.st%k;
aux[i][jp] = min(aux[i][jp], w + v.nd);
}
}
}
}
void combine(int u, int p){
int subu = u;
int subv = u + (1<<p);
fr(mid, 0, k){
fr(i, 0, k){
fr(j, 0, k){
sparse[subu][p+1][i][j] = min(sparse[subu][p+1][i][j], aux[i][mid] + sparse[subv][p][mid][j]);
}
}
}
}
void update(int u, int p){
if(u + (1<<p) > n) return;
fillaux(u, p-1);
combine(u, p-1);
}
ll dist[5];
ll dist2[5];
ll query(int a, int b){
int S = a/k;
int E = b/k;
if(S == E) return -1;
fr(i, 0, k) dist[i] = inf;
dist[a%k] = 0;
for(int p = 16; p >= 0; p --){
while(S + (1<<p) <= E){
fr(i, 0, k) dist2[i] = inf;
fr(i, 0, k){
fr(j, 0, k){
ll w = sparse[S][p][i][j];
int pos = (S+(1<<p)-1)*k + j;
for(auto v : g[pos]){
int jp = v.st%k;
dist2[jp] = min(dist2[jp], dist[i] + w + v.nd);
}
}
}
fr(i, 0, k) dist[i] = dist2[i];
S += (1<<p);
}
}
if(dist[b%k] == inf) return -1;
else return dist[b%k];
}
void solve(){
cin >> k >> n >> m >> q;
while(n % k != 0) ++n;
fr(i, 0, m){
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
}
fr(u, 0, n/k){
fr(p, 0, 17){
fr(i, 0, k){
fr(j, 0, k){
sparse[u][p][i][j] = inf;
}
}
}
}
fr(u, 0, n/k){
fr(i, 0, k){
sparse[u][0][i][i] = 0;
}
}
fr(p, 1, 17){
fr(u, 0, n/k){
update(u, p);
}
}
while(q--){
int a, b;
cin >> a >> b;
cout<<query(a, b)<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
215 ms |
172984 KB |
Output is correct |
2 |
Correct |
5 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
9 ms |
8268 KB |
Output is correct |
6 |
Correct |
8 ms |
8260 KB |
Output is correct |
7 |
Correct |
11 ms |
8268 KB |
Output is correct |
8 |
Correct |
187 ms |
172908 KB |
Output is correct |
9 |
Correct |
171 ms |
172740 KB |
Output is correct |
10 |
Correct |
135 ms |
171396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
311 ms |
90692 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
28 ms |
8392 KB |
Output is correct |
8 |
Correct |
30 ms |
6860 KB |
Output is correct |
9 |
Correct |
179 ms |
172900 KB |
Output is correct |
10 |
Correct |
363 ms |
64596 KB |
Output is correct |
11 |
Correct |
302 ms |
91048 KB |
Output is correct |
12 |
Correct |
266 ms |
62612 KB |
Output is correct |
13 |
Correct |
276 ms |
29652 KB |
Output is correct |
14 |
Correct |
190 ms |
40912 KB |
Output is correct |
15 |
Correct |
175 ms |
27048 KB |
Output is correct |
16 |
Correct |
177 ms |
27036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
5 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
5 ms |
4940 KB |
Output is correct |
6 |
Correct |
9 ms |
8268 KB |
Output is correct |
7 |
Correct |
6 ms |
6660 KB |
Output is correct |
8 |
Correct |
12 ms |
5836 KB |
Output is correct |
9 |
Correct |
10 ms |
5944 KB |
Output is correct |
10 |
Correct |
143 ms |
172864 KB |
Output is correct |
11 |
Correct |
207 ms |
90756 KB |
Output is correct |
12 |
Correct |
253 ms |
64436 KB |
Output is correct |
13 |
Correct |
281 ms |
64956 KB |
Output is correct |
14 |
Correct |
213 ms |
63688 KB |
Output is correct |
15 |
Correct |
141 ms |
26996 KB |
Output is correct |
16 |
Correct |
147 ms |
26952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
5 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
5 ms |
4940 KB |
Output is correct |
6 |
Correct |
9 ms |
8268 KB |
Output is correct |
7 |
Correct |
6 ms |
6660 KB |
Output is correct |
8 |
Correct |
12 ms |
5836 KB |
Output is correct |
9 |
Correct |
10 ms |
5944 KB |
Output is correct |
10 |
Correct |
143 ms |
172864 KB |
Output is correct |
11 |
Correct |
207 ms |
90756 KB |
Output is correct |
12 |
Correct |
253 ms |
64436 KB |
Output is correct |
13 |
Correct |
281 ms |
64956 KB |
Output is correct |
14 |
Correct |
213 ms |
63688 KB |
Output is correct |
15 |
Correct |
141 ms |
26996 KB |
Output is correct |
16 |
Correct |
147 ms |
26952 KB |
Output is correct |
17 |
Correct |
215 ms |
90688 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
4940 KB |
Output is correct |
22 |
Correct |
4 ms |
4940 KB |
Output is correct |
23 |
Correct |
12 ms |
8268 KB |
Output is correct |
24 |
Correct |
13 ms |
6732 KB |
Output is correct |
25 |
Correct |
29 ms |
5708 KB |
Output is correct |
26 |
Correct |
19 ms |
5964 KB |
Output is correct |
27 |
Correct |
185 ms |
172904 KB |
Output is correct |
28 |
Correct |
335 ms |
64516 KB |
Output is correct |
29 |
Correct |
354 ms |
64988 KB |
Output is correct |
30 |
Correct |
290 ms |
63712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
215 ms |
172984 KB |
Output is correct |
2 |
Correct |
5 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
9 ms |
8268 KB |
Output is correct |
6 |
Correct |
8 ms |
8260 KB |
Output is correct |
7 |
Correct |
11 ms |
8268 KB |
Output is correct |
8 |
Correct |
187 ms |
172908 KB |
Output is correct |
9 |
Correct |
171 ms |
172740 KB |
Output is correct |
10 |
Correct |
135 ms |
171396 KB |
Output is correct |
11 |
Correct |
311 ms |
90692 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
28 ms |
8392 KB |
Output is correct |
18 |
Correct |
30 ms |
6860 KB |
Output is correct |
19 |
Correct |
179 ms |
172900 KB |
Output is correct |
20 |
Correct |
363 ms |
64596 KB |
Output is correct |
21 |
Correct |
302 ms |
91048 KB |
Output is correct |
22 |
Correct |
266 ms |
62612 KB |
Output is correct |
23 |
Correct |
276 ms |
29652 KB |
Output is correct |
24 |
Correct |
190 ms |
40912 KB |
Output is correct |
25 |
Correct |
175 ms |
27048 KB |
Output is correct |
26 |
Correct |
177 ms |
27036 KB |
Output is correct |
27 |
Correct |
4 ms |
4940 KB |
Output is correct |
28 |
Correct |
4 ms |
4940 KB |
Output is correct |
29 |
Correct |
5 ms |
4940 KB |
Output is correct |
30 |
Correct |
3 ms |
4940 KB |
Output is correct |
31 |
Correct |
5 ms |
4940 KB |
Output is correct |
32 |
Correct |
9 ms |
8268 KB |
Output is correct |
33 |
Correct |
6 ms |
6660 KB |
Output is correct |
34 |
Correct |
12 ms |
5836 KB |
Output is correct |
35 |
Correct |
10 ms |
5944 KB |
Output is correct |
36 |
Correct |
143 ms |
172864 KB |
Output is correct |
37 |
Correct |
207 ms |
90756 KB |
Output is correct |
38 |
Correct |
253 ms |
64436 KB |
Output is correct |
39 |
Correct |
281 ms |
64956 KB |
Output is correct |
40 |
Correct |
213 ms |
63688 KB |
Output is correct |
41 |
Correct |
141 ms |
26996 KB |
Output is correct |
42 |
Correct |
147 ms |
26952 KB |
Output is correct |
43 |
Correct |
215 ms |
90688 KB |
Output is correct |
44 |
Correct |
4 ms |
4940 KB |
Output is correct |
45 |
Correct |
4 ms |
4940 KB |
Output is correct |
46 |
Correct |
4 ms |
4940 KB |
Output is correct |
47 |
Correct |
4 ms |
4940 KB |
Output is correct |
48 |
Correct |
4 ms |
4940 KB |
Output is correct |
49 |
Correct |
12 ms |
8268 KB |
Output is correct |
50 |
Correct |
13 ms |
6732 KB |
Output is correct |
51 |
Correct |
29 ms |
5708 KB |
Output is correct |
52 |
Correct |
19 ms |
5964 KB |
Output is correct |
53 |
Correct |
185 ms |
172904 KB |
Output is correct |
54 |
Correct |
335 ms |
64516 KB |
Output is correct |
55 |
Correct |
354 ms |
64988 KB |
Output is correct |
56 |
Correct |
290 ms |
63712 KB |
Output is correct |
57 |
Correct |
422 ms |
51060 KB |
Output is correct |
58 |
Correct |
192 ms |
173824 KB |
Output is correct |
59 |
Correct |
332 ms |
92632 KB |
Output is correct |