#include <bits/stdc++.h>
#pragma GCC target("sse,sse2")
#pragma GCC optimize("unroll-loops,O2")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 5e4+10,mod = 998244353,inf = 1e9+10;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k /= 2;
}
return z;
}
int n,m,k;
vector<pll> adj[N];
int a[N][6][6];
int seg[N*4][6][6];
int co[5][5],cur[5][5];
vector<int> ve;
void build(int l,int r,int v = 1){
if (r-l == 1){
rep(i,0,k){
rep(j,0,k){
seg[v][i][j] = a[l][i][j];
}
}
return;
}
int u = (v << 1),m = (l+r) >> 1;
build(l,m,u);
build(m,r,u|1);
rep(i,0,k){
rep(j,0,k){
rep(x,0,k){
seg[v][i][j] = min(seg[v][i][j],seg[u][i][x]+seg[u|1][x][j]);
}
}
}
}
void que(int l,int r,int s,int e,int v = 1){
if (s <= l && r <= e){
ve.pb(v);
return;
}
if (s >= r || l >= e) return;
int u = (v << 1),m = (l+r) >> 1;
que(l,m,s,e,u);
que(m,r,s,e,u|1);
}
int main(){
// ios :: sync_with_stdio(0); cin.tie(0);
memset(seg,63,sizeof seg);
memset(a,63,sizeof a);
int q;
cin >> k >> n >> m >> q;
rep(i,0,m){
int u,v,w;
cin >> u >> v >> w;
adj[u].pb({v,w});
}
rep(i,0,n){
for (pll p : adj[i]){
int x = i%k,y = p.X%k;
a[i/k][x][y] = min(a[i/k][x][y],p.Y);
}
}
int sz = (n-1)/k;
build(0,sz+1);
while (q--){
int x,y;
cin >> x >> y;
if (x/k == y/k){
cout << -1 << endl;
continue;
}
ve.clear();
que(0,sz+1,x/k,y/k);
bool f = 0;
for (int v : ve){
if (!f){
rep(i,0,k)
rep(j,0,k)
cur[i][j] = seg[v][i][j];
f = 1;
continue;
}
memset(co,63,sizeof co);
rep(i,0,k){
rep(j,0,k){
rep(z,0,k){
co[i][j] = min(co[i][j],cur[i][z]+seg[v][z][j]);
}
}
}
rep(i,0,k) rep(j,0,k) cur[i][j] = co[i][j];
}
if (cur[x%k][y%k] > inf) cout << -1 << endl;
else cout << cur[x%k][y%k] << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
39224 KB |
Output is correct |
2 |
Correct |
16 ms |
36692 KB |
Output is correct |
3 |
Correct |
16 ms |
36616 KB |
Output is correct |
4 |
Correct |
19 ms |
36692 KB |
Output is correct |
5 |
Correct |
18 ms |
36692 KB |
Output is correct |
6 |
Correct |
20 ms |
36708 KB |
Output is correct |
7 |
Correct |
19 ms |
36760 KB |
Output is correct |
8 |
Correct |
86 ms |
39204 KB |
Output is correct |
9 |
Correct |
88 ms |
39016 KB |
Output is correct |
10 |
Correct |
43 ms |
36772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
39812 KB |
Output is correct |
2 |
Correct |
17 ms |
36692 KB |
Output is correct |
3 |
Correct |
19 ms |
36596 KB |
Output is correct |
4 |
Correct |
16 ms |
36720 KB |
Output is correct |
5 |
Correct |
17 ms |
36692 KB |
Output is correct |
6 |
Correct |
16 ms |
36692 KB |
Output is correct |
7 |
Correct |
35 ms |
36824 KB |
Output is correct |
8 |
Correct |
47 ms |
36852 KB |
Output is correct |
9 |
Correct |
86 ms |
39144 KB |
Output is correct |
10 |
Correct |
147 ms |
41392 KB |
Output is correct |
11 |
Correct |
121 ms |
40016 KB |
Output is correct |
12 |
Correct |
99 ms |
39376 KB |
Output is correct |
13 |
Correct |
163 ms |
41696 KB |
Output is correct |
14 |
Correct |
98 ms |
39724 KB |
Output is correct |
15 |
Correct |
87 ms |
39224 KB |
Output is correct |
16 |
Correct |
97 ms |
39224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
36692 KB |
Output is correct |
2 |
Correct |
17 ms |
36652 KB |
Output is correct |
3 |
Correct |
20 ms |
36596 KB |
Output is correct |
4 |
Correct |
16 ms |
36628 KB |
Output is correct |
5 |
Correct |
18 ms |
36712 KB |
Output is correct |
6 |
Correct |
17 ms |
36740 KB |
Output is correct |
7 |
Correct |
21 ms |
36676 KB |
Output is correct |
8 |
Correct |
23 ms |
36820 KB |
Output is correct |
9 |
Correct |
18 ms |
36732 KB |
Output is correct |
10 |
Correct |
64 ms |
39016 KB |
Output is correct |
11 |
Correct |
97 ms |
39716 KB |
Output is correct |
12 |
Correct |
159 ms |
41280 KB |
Output is correct |
13 |
Correct |
147 ms |
41760 KB |
Output is correct |
14 |
Correct |
116 ms |
40560 KB |
Output is correct |
15 |
Correct |
80 ms |
39148 KB |
Output is correct |
16 |
Correct |
108 ms |
39100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
36692 KB |
Output is correct |
2 |
Correct |
17 ms |
36652 KB |
Output is correct |
3 |
Correct |
20 ms |
36596 KB |
Output is correct |
4 |
Correct |
16 ms |
36628 KB |
Output is correct |
5 |
Correct |
18 ms |
36712 KB |
Output is correct |
6 |
Correct |
17 ms |
36740 KB |
Output is correct |
7 |
Correct |
21 ms |
36676 KB |
Output is correct |
8 |
Correct |
23 ms |
36820 KB |
Output is correct |
9 |
Correct |
18 ms |
36732 KB |
Output is correct |
10 |
Correct |
64 ms |
39016 KB |
Output is correct |
11 |
Correct |
97 ms |
39716 KB |
Output is correct |
12 |
Correct |
159 ms |
41280 KB |
Output is correct |
13 |
Correct |
147 ms |
41760 KB |
Output is correct |
14 |
Correct |
116 ms |
40560 KB |
Output is correct |
15 |
Correct |
80 ms |
39148 KB |
Output is correct |
16 |
Correct |
108 ms |
39100 KB |
Output is correct |
17 |
Correct |
116 ms |
39824 KB |
Output is correct |
18 |
Correct |
17 ms |
36692 KB |
Output is correct |
19 |
Correct |
16 ms |
36692 KB |
Output is correct |
20 |
Correct |
16 ms |
36636 KB |
Output is correct |
21 |
Correct |
16 ms |
36692 KB |
Output is correct |
22 |
Correct |
16 ms |
36592 KB |
Output is correct |
23 |
Correct |
22 ms |
36724 KB |
Output is correct |
24 |
Correct |
26 ms |
36700 KB |
Output is correct |
25 |
Correct |
31 ms |
36844 KB |
Output is correct |
26 |
Correct |
35 ms |
36724 KB |
Output is correct |
27 |
Correct |
72 ms |
39104 KB |
Output is correct |
28 |
Correct |
151 ms |
41324 KB |
Output is correct |
29 |
Correct |
155 ms |
41764 KB |
Output is correct |
30 |
Correct |
127 ms |
40580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
39224 KB |
Output is correct |
2 |
Correct |
16 ms |
36692 KB |
Output is correct |
3 |
Correct |
16 ms |
36616 KB |
Output is correct |
4 |
Correct |
19 ms |
36692 KB |
Output is correct |
5 |
Correct |
18 ms |
36692 KB |
Output is correct |
6 |
Correct |
20 ms |
36708 KB |
Output is correct |
7 |
Correct |
19 ms |
36760 KB |
Output is correct |
8 |
Correct |
86 ms |
39204 KB |
Output is correct |
9 |
Correct |
88 ms |
39016 KB |
Output is correct |
10 |
Correct |
43 ms |
36772 KB |
Output is correct |
11 |
Correct |
139 ms |
39812 KB |
Output is correct |
12 |
Correct |
17 ms |
36692 KB |
Output is correct |
13 |
Correct |
19 ms |
36596 KB |
Output is correct |
14 |
Correct |
16 ms |
36720 KB |
Output is correct |
15 |
Correct |
17 ms |
36692 KB |
Output is correct |
16 |
Correct |
16 ms |
36692 KB |
Output is correct |
17 |
Correct |
35 ms |
36824 KB |
Output is correct |
18 |
Correct |
47 ms |
36852 KB |
Output is correct |
19 |
Correct |
86 ms |
39144 KB |
Output is correct |
20 |
Correct |
147 ms |
41392 KB |
Output is correct |
21 |
Correct |
121 ms |
40016 KB |
Output is correct |
22 |
Correct |
99 ms |
39376 KB |
Output is correct |
23 |
Correct |
163 ms |
41696 KB |
Output is correct |
24 |
Correct |
98 ms |
39724 KB |
Output is correct |
25 |
Correct |
87 ms |
39224 KB |
Output is correct |
26 |
Correct |
97 ms |
39224 KB |
Output is correct |
27 |
Correct |
17 ms |
36692 KB |
Output is correct |
28 |
Correct |
17 ms |
36652 KB |
Output is correct |
29 |
Correct |
20 ms |
36596 KB |
Output is correct |
30 |
Correct |
16 ms |
36628 KB |
Output is correct |
31 |
Correct |
18 ms |
36712 KB |
Output is correct |
32 |
Correct |
17 ms |
36740 KB |
Output is correct |
33 |
Correct |
21 ms |
36676 KB |
Output is correct |
34 |
Correct |
23 ms |
36820 KB |
Output is correct |
35 |
Correct |
18 ms |
36732 KB |
Output is correct |
36 |
Correct |
64 ms |
39016 KB |
Output is correct |
37 |
Correct |
97 ms |
39716 KB |
Output is correct |
38 |
Correct |
159 ms |
41280 KB |
Output is correct |
39 |
Correct |
147 ms |
41760 KB |
Output is correct |
40 |
Correct |
116 ms |
40560 KB |
Output is correct |
41 |
Correct |
80 ms |
39148 KB |
Output is correct |
42 |
Correct |
108 ms |
39100 KB |
Output is correct |
43 |
Correct |
116 ms |
39824 KB |
Output is correct |
44 |
Correct |
17 ms |
36692 KB |
Output is correct |
45 |
Correct |
16 ms |
36692 KB |
Output is correct |
46 |
Correct |
16 ms |
36636 KB |
Output is correct |
47 |
Correct |
16 ms |
36692 KB |
Output is correct |
48 |
Correct |
16 ms |
36592 KB |
Output is correct |
49 |
Correct |
22 ms |
36724 KB |
Output is correct |
50 |
Correct |
26 ms |
36700 KB |
Output is correct |
51 |
Correct |
31 ms |
36844 KB |
Output is correct |
52 |
Correct |
35 ms |
36724 KB |
Output is correct |
53 |
Correct |
72 ms |
39104 KB |
Output is correct |
54 |
Correct |
151 ms |
41324 KB |
Output is correct |
55 |
Correct |
155 ms |
41764 KB |
Output is correct |
56 |
Correct |
127 ms |
40580 KB |
Output is correct |
57 |
Correct |
219 ms |
42728 KB |
Output is correct |
58 |
Correct |
89 ms |
39132 KB |
Output is correct |
59 |
Correct |
121 ms |
40016 KB |
Output is correct |