#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 = 1e5;
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 |
192 ms |
170564 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2636 KB |
Output is correct |
4 |
Correct |
3 ms |
2636 KB |
Output is correct |
5 |
Correct |
7 ms |
6016 KB |
Output is correct |
6 |
Correct |
8 ms |
5964 KB |
Output is correct |
7 |
Correct |
7 ms |
5964 KB |
Output is correct |
8 |
Correct |
203 ms |
170524 KB |
Output is correct |
9 |
Correct |
188 ms |
170596 KB |
Output is correct |
10 |
Correct |
144 ms |
168988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
273 ms |
88352 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2636 KB |
Output is correct |
4 |
Correct |
3 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
2636 KB |
Output is correct |
6 |
Correct |
3 ms |
2664 KB |
Output is correct |
7 |
Correct |
33 ms |
6044 KB |
Output is correct |
8 |
Correct |
29 ms |
4416 KB |
Output is correct |
9 |
Correct |
194 ms |
170524 KB |
Output is correct |
10 |
Correct |
370 ms |
62236 KB |
Output is correct |
11 |
Correct |
235 ms |
88460 KB |
Output is correct |
12 |
Correct |
217 ms |
60356 KB |
Output is correct |
13 |
Correct |
275 ms |
27352 KB |
Output is correct |
14 |
Correct |
159 ms |
38492 KB |
Output is correct |
15 |
Correct |
171 ms |
24604 KB |
Output is correct |
16 |
Correct |
155 ms |
24800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
5 ms |
5964 KB |
Output is correct |
7 |
Correct |
6 ms |
4300 KB |
Output is correct |
8 |
Correct |
8 ms |
3404 KB |
Output is correct |
9 |
Correct |
7 ms |
3604 KB |
Output is correct |
10 |
Correct |
169 ms |
170552 KB |
Output is correct |
11 |
Correct |
262 ms |
88348 KB |
Output is correct |
12 |
Correct |
277 ms |
62092 KB |
Output is correct |
13 |
Correct |
341 ms |
62532 KB |
Output is correct |
14 |
Correct |
222 ms |
61304 KB |
Output is correct |
15 |
Correct |
146 ms |
24668 KB |
Output is correct |
16 |
Correct |
129 ms |
24644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
5 ms |
5964 KB |
Output is correct |
7 |
Correct |
6 ms |
4300 KB |
Output is correct |
8 |
Correct |
8 ms |
3404 KB |
Output is correct |
9 |
Correct |
7 ms |
3604 KB |
Output is correct |
10 |
Correct |
169 ms |
170552 KB |
Output is correct |
11 |
Correct |
262 ms |
88348 KB |
Output is correct |
12 |
Correct |
277 ms |
62092 KB |
Output is correct |
13 |
Correct |
341 ms |
62532 KB |
Output is correct |
14 |
Correct |
222 ms |
61304 KB |
Output is correct |
15 |
Correct |
146 ms |
24668 KB |
Output is correct |
16 |
Correct |
129 ms |
24644 KB |
Output is correct |
17 |
Correct |
249 ms |
88452 KB |
Output is correct |
18 |
Correct |
3 ms |
2688 KB |
Output is correct |
19 |
Correct |
3 ms |
2636 KB |
Output is correct |
20 |
Correct |
3 ms |
2636 KB |
Output is correct |
21 |
Correct |
3 ms |
2636 KB |
Output is correct |
22 |
Correct |
3 ms |
2636 KB |
Output is correct |
23 |
Correct |
14 ms |
6040 KB |
Output is correct |
24 |
Correct |
14 ms |
4412 KB |
Output is correct |
25 |
Correct |
21 ms |
3468 KB |
Output is correct |
26 |
Correct |
17 ms |
3616 KB |
Output is correct |
27 |
Correct |
175 ms |
170456 KB |
Output is correct |
28 |
Correct |
316 ms |
62160 KB |
Output is correct |
29 |
Correct |
338 ms |
62512 KB |
Output is correct |
30 |
Correct |
232 ms |
61332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
170564 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2636 KB |
Output is correct |
4 |
Correct |
3 ms |
2636 KB |
Output is correct |
5 |
Correct |
7 ms |
6016 KB |
Output is correct |
6 |
Correct |
8 ms |
5964 KB |
Output is correct |
7 |
Correct |
7 ms |
5964 KB |
Output is correct |
8 |
Correct |
203 ms |
170524 KB |
Output is correct |
9 |
Correct |
188 ms |
170596 KB |
Output is correct |
10 |
Correct |
144 ms |
168988 KB |
Output is correct |
11 |
Correct |
273 ms |
88352 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
3 ms |
2636 KB |
Output is correct |
14 |
Correct |
3 ms |
2636 KB |
Output is correct |
15 |
Correct |
3 ms |
2636 KB |
Output is correct |
16 |
Correct |
3 ms |
2664 KB |
Output is correct |
17 |
Correct |
33 ms |
6044 KB |
Output is correct |
18 |
Correct |
29 ms |
4416 KB |
Output is correct |
19 |
Correct |
194 ms |
170524 KB |
Output is correct |
20 |
Correct |
370 ms |
62236 KB |
Output is correct |
21 |
Correct |
235 ms |
88460 KB |
Output is correct |
22 |
Correct |
217 ms |
60356 KB |
Output is correct |
23 |
Correct |
275 ms |
27352 KB |
Output is correct |
24 |
Correct |
159 ms |
38492 KB |
Output is correct |
25 |
Correct |
171 ms |
24604 KB |
Output is correct |
26 |
Correct |
155 ms |
24800 KB |
Output is correct |
27 |
Correct |
3 ms |
2636 KB |
Output is correct |
28 |
Correct |
3 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
2 ms |
2636 KB |
Output is correct |
32 |
Correct |
5 ms |
5964 KB |
Output is correct |
33 |
Correct |
6 ms |
4300 KB |
Output is correct |
34 |
Correct |
8 ms |
3404 KB |
Output is correct |
35 |
Correct |
7 ms |
3604 KB |
Output is correct |
36 |
Correct |
169 ms |
170552 KB |
Output is correct |
37 |
Correct |
262 ms |
88348 KB |
Output is correct |
38 |
Correct |
277 ms |
62092 KB |
Output is correct |
39 |
Correct |
341 ms |
62532 KB |
Output is correct |
40 |
Correct |
222 ms |
61304 KB |
Output is correct |
41 |
Correct |
146 ms |
24668 KB |
Output is correct |
42 |
Correct |
129 ms |
24644 KB |
Output is correct |
43 |
Correct |
249 ms |
88452 KB |
Output is correct |
44 |
Correct |
3 ms |
2688 KB |
Output is correct |
45 |
Correct |
3 ms |
2636 KB |
Output is correct |
46 |
Correct |
3 ms |
2636 KB |
Output is correct |
47 |
Correct |
3 ms |
2636 KB |
Output is correct |
48 |
Correct |
3 ms |
2636 KB |
Output is correct |
49 |
Correct |
14 ms |
6040 KB |
Output is correct |
50 |
Correct |
14 ms |
4412 KB |
Output is correct |
51 |
Correct |
21 ms |
3468 KB |
Output is correct |
52 |
Correct |
17 ms |
3616 KB |
Output is correct |
53 |
Correct |
175 ms |
170456 KB |
Output is correct |
54 |
Correct |
316 ms |
62160 KB |
Output is correct |
55 |
Correct |
338 ms |
62512 KB |
Output is correct |
56 |
Correct |
232 ms |
61332 KB |
Output is correct |
57 |
Runtime error |
401 ms |
98280 KB |
Execution killed with signal 11 |
58 |
Halted |
0 ms |
0 KB |
- |