#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 query(int a, int b){
int S = a/k;
int E = b/k;
if(S == E) return -1;
ll dist[k];
fr(i, 0, k) dist[i] = inf;
dist[a%k] = 0;
for(int p = 16; p >= 0; p --){
while(S + (1<<p) <= E){
ll dist2[k];
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 |
170 ms |
171516 KB |
Output is correct |
2 |
Correct |
3 ms |
2672 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
3 ms |
2636 KB |
Output is correct |
5 |
Correct |
6 ms |
6004 KB |
Output is correct |
6 |
Correct |
7 ms |
5964 KB |
Output is correct |
7 |
Correct |
7 ms |
6008 KB |
Output is correct |
8 |
Correct |
165 ms |
171468 KB |
Output is correct |
9 |
Correct |
166 ms |
171408 KB |
Output is correct |
10 |
Correct |
144 ms |
169128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
90052 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 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 |
2676 KB |
Output is correct |
7 |
Correct |
25 ms |
6116 KB |
Output is correct |
8 |
Correct |
28 ms |
4428 KB |
Output is correct |
9 |
Correct |
161 ms |
171484 KB |
Output is correct |
10 |
Correct |
290 ms |
64476 KB |
Output is correct |
11 |
Correct |
240 ms |
90260 KB |
Output is correct |
12 |
Correct |
202 ms |
61564 KB |
Output is correct |
13 |
Correct |
283 ms |
29816 KB |
Output is correct |
14 |
Correct |
148 ms |
40040 KB |
Output is correct |
15 |
Correct |
148 ms |
25900 KB |
Output is correct |
16 |
Correct |
163 ms |
25948 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 |
3 ms |
2636 KB |
Output is correct |
4 |
Correct |
3 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 |
5 ms |
4428 KB |
Output is correct |
8 |
Correct |
8 ms |
3532 KB |
Output is correct |
9 |
Correct |
6 ms |
3532 KB |
Output is correct |
10 |
Correct |
140 ms |
171440 KB |
Output is correct |
11 |
Correct |
266 ms |
89860 KB |
Output is correct |
12 |
Correct |
293 ms |
64196 KB |
Output is correct |
13 |
Correct |
255 ms |
64996 KB |
Output is correct |
14 |
Correct |
252 ms |
63300 KB |
Output is correct |
15 |
Correct |
152 ms |
25796 KB |
Output is correct |
16 |
Correct |
127 ms |
25792 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 |
3 ms |
2636 KB |
Output is correct |
4 |
Correct |
3 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 |
5 ms |
4428 KB |
Output is correct |
8 |
Correct |
8 ms |
3532 KB |
Output is correct |
9 |
Correct |
6 ms |
3532 KB |
Output is correct |
10 |
Correct |
140 ms |
171440 KB |
Output is correct |
11 |
Correct |
266 ms |
89860 KB |
Output is correct |
12 |
Correct |
293 ms |
64196 KB |
Output is correct |
13 |
Correct |
255 ms |
64996 KB |
Output is correct |
14 |
Correct |
252 ms |
63300 KB |
Output is correct |
15 |
Correct |
152 ms |
25796 KB |
Output is correct |
16 |
Correct |
127 ms |
25792 KB |
Output is correct |
17 |
Correct |
286 ms |
89956 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
2 ms |
2676 KB |
Output is correct |
21 |
Correct |
3 ms |
2636 KB |
Output is correct |
22 |
Correct |
2 ms |
2668 KB |
Output is correct |
23 |
Correct |
11 ms |
6012 KB |
Output is correct |
24 |
Correct |
12 ms |
4460 KB |
Output is correct |
25 |
Correct |
21 ms |
3532 KB |
Output is correct |
26 |
Correct |
17 ms |
3660 KB |
Output is correct |
27 |
Correct |
148 ms |
171400 KB |
Output is correct |
28 |
Correct |
248 ms |
64336 KB |
Output is correct |
29 |
Correct |
344 ms |
64992 KB |
Output is correct |
30 |
Correct |
221 ms |
63148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
171516 KB |
Output is correct |
2 |
Correct |
3 ms |
2672 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
3 ms |
2636 KB |
Output is correct |
5 |
Correct |
6 ms |
6004 KB |
Output is correct |
6 |
Correct |
7 ms |
5964 KB |
Output is correct |
7 |
Correct |
7 ms |
6008 KB |
Output is correct |
8 |
Correct |
165 ms |
171468 KB |
Output is correct |
9 |
Correct |
166 ms |
171408 KB |
Output is correct |
10 |
Correct |
144 ms |
169128 KB |
Output is correct |
11 |
Correct |
236 ms |
90052 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 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 |
2676 KB |
Output is correct |
17 |
Correct |
25 ms |
6116 KB |
Output is correct |
18 |
Correct |
28 ms |
4428 KB |
Output is correct |
19 |
Correct |
161 ms |
171484 KB |
Output is correct |
20 |
Correct |
290 ms |
64476 KB |
Output is correct |
21 |
Correct |
240 ms |
90260 KB |
Output is correct |
22 |
Correct |
202 ms |
61564 KB |
Output is correct |
23 |
Correct |
283 ms |
29816 KB |
Output is correct |
24 |
Correct |
148 ms |
40040 KB |
Output is correct |
25 |
Correct |
148 ms |
25900 KB |
Output is correct |
26 |
Correct |
163 ms |
25948 KB |
Output is correct |
27 |
Correct |
3 ms |
2636 KB |
Output is correct |
28 |
Correct |
3 ms |
2636 KB |
Output is correct |
29 |
Correct |
3 ms |
2636 KB |
Output is correct |
30 |
Correct |
3 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 |
5 ms |
4428 KB |
Output is correct |
34 |
Correct |
8 ms |
3532 KB |
Output is correct |
35 |
Correct |
6 ms |
3532 KB |
Output is correct |
36 |
Correct |
140 ms |
171440 KB |
Output is correct |
37 |
Correct |
266 ms |
89860 KB |
Output is correct |
38 |
Correct |
293 ms |
64196 KB |
Output is correct |
39 |
Correct |
255 ms |
64996 KB |
Output is correct |
40 |
Correct |
252 ms |
63300 KB |
Output is correct |
41 |
Correct |
152 ms |
25796 KB |
Output is correct |
42 |
Correct |
127 ms |
25792 KB |
Output is correct |
43 |
Correct |
286 ms |
89956 KB |
Output is correct |
44 |
Correct |
2 ms |
2636 KB |
Output is correct |
45 |
Correct |
2 ms |
2636 KB |
Output is correct |
46 |
Correct |
2 ms |
2676 KB |
Output is correct |
47 |
Correct |
3 ms |
2636 KB |
Output is correct |
48 |
Correct |
2 ms |
2668 KB |
Output is correct |
49 |
Correct |
11 ms |
6012 KB |
Output is correct |
50 |
Correct |
12 ms |
4460 KB |
Output is correct |
51 |
Correct |
21 ms |
3532 KB |
Output is correct |
52 |
Correct |
17 ms |
3660 KB |
Output is correct |
53 |
Correct |
148 ms |
171400 KB |
Output is correct |
54 |
Correct |
248 ms |
64336 KB |
Output is correct |
55 |
Correct |
344 ms |
64992 KB |
Output is correct |
56 |
Correct |
221 ms |
63148 KB |
Output is correct |
57 |
Runtime error |
358 ms |
101660 KB |
Execution killed with signal 11 |
58 |
Halted |
0 ms |
0 KB |
- |