#include <bits/stdc++.h>
#define vi vector<int>
#define ve vector
#define ll long long
#define vf vector<float>
#define vll vector<pair<ll,ll>>
#define ii pair<int,int>
#define pll pair<ll,ll>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e9
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 2e5+5
using namespace std;
const int MOD = 1e9+7;
const int nax = 5e4+5;
const int nax2 = 20;
typedef complex<int> Point;
using cd = complex<double>;
int k,n,m,o, dat[5][5][32][nax];
int ans[5][5],cnt[5][5];
void combine(int l, int lev){
for(int i = 0; i < k; i++)
for(int j = 0; j < k; j++){
for (int z = 0; z < k; ++z)
{
dat[i][j][lev][l] = min(dat[i][j][lev][l], dat[i][z][lev-1][l] + dat[z][j][lev-1][l+(1 << (lev-1))]);
}
}
}
int main()
{
optimise;
cin >> k >> n >> m >> o;
for (int i = 0; i < k; ++i)
for (int j = 0; j < k; ++j)
for (int a = 0; a < 25; ++a)
for(int b = 0; b < n; b++)
dat[i][j][a][b] = INF;
for (int i = 0; i < m; ++i)
{
int a,b,c;
cin >> a >> b >> c;
dat[a % k][b % k][0][a / k] = c;
}
for (int i = 1; i < 25; ++i)
{
for (int j = 0; j + (1 << i) < (n+k-1)/k; ++j)
{
combine(j, i);
}
}
while(o--){
int a,b;
cin >> a >> b;
if(a == b) cout << 0 << endl;
else if(a/k >= b/k) cout << -1 << endl;
else {
int cur = a/k;
for(int i = 0; i < k; i++)
for(int j = 0; j < k; j++)
if(i == j) ans[i][j] = 0;
else ans[i][j] = INF;
for (int lvl = 20; lvl >= 0; --lvl)
{
if(cur + (1 << lvl) <= b/k){
for(int i = 0; i < k; i++)
for(int j = 0; j < k; j++)
cnt[i][j] = INF;
for(int i = 0; i < k; i++)
for(int j = 0; j < k; j++){
for (int z = 0; z < k; ++z)
{
cnt[i][j] = min(cnt[i][j], ans[i][z] + dat[z][j][lvl][cur]);
}
}
memcpy(ans, cnt, sizeof ans);
cur += (1 << lvl);
}
}
if(ans[a%k][b%k] >= INF) cout << -1 << endl;
else cout << ans[a%k][b%k] << endl;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
5640 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
472 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
34 ms |
6100 KB |
Output is correct |
9 |
Correct |
40 ms |
6100 KB |
Output is correct |
10 |
Correct |
23 ms |
5344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
20284 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
2004 KB |
Output is correct |
6 |
Correct |
2 ms |
3028 KB |
Output is correct |
7 |
Correct |
15 ms |
596 KB |
Output is correct |
8 |
Correct |
16 ms |
1264 KB |
Output is correct |
9 |
Correct |
34 ms |
6092 KB |
Output is correct |
10 |
Correct |
80 ms |
46700 KB |
Output is correct |
11 |
Correct |
53 ms |
21568 KB |
Output is correct |
12 |
Correct |
64 ms |
45692 KB |
Output is correct |
13 |
Correct |
86 ms |
78600 KB |
Output is correct |
14 |
Correct |
45 ms |
29132 KB |
Output is correct |
15 |
Correct |
74 ms |
77516 KB |
Output is correct |
16 |
Correct |
73 ms |
77448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
2004 KB |
Output is correct |
5 |
Correct |
1 ms |
3028 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
1196 KB |
Output is correct |
8 |
Correct |
4 ms |
5608 KB |
Output is correct |
9 |
Correct |
3 ms |
3668 KB |
Output is correct |
10 |
Correct |
21 ms |
5972 KB |
Output is correct |
11 |
Correct |
37 ms |
21428 KB |
Output is correct |
12 |
Correct |
60 ms |
46560 KB |
Output is correct |
13 |
Correct |
66 ms |
46816 KB |
Output is correct |
14 |
Correct |
54 ms |
46096 KB |
Output is correct |
15 |
Correct |
61 ms |
77480 KB |
Output is correct |
16 |
Correct |
68 ms |
77388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
2004 KB |
Output is correct |
5 |
Correct |
1 ms |
3028 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
1196 KB |
Output is correct |
8 |
Correct |
4 ms |
5608 KB |
Output is correct |
9 |
Correct |
3 ms |
3668 KB |
Output is correct |
10 |
Correct |
21 ms |
5972 KB |
Output is correct |
11 |
Correct |
37 ms |
21428 KB |
Output is correct |
12 |
Correct |
60 ms |
46560 KB |
Output is correct |
13 |
Correct |
66 ms |
46816 KB |
Output is correct |
14 |
Correct |
54 ms |
46096 KB |
Output is correct |
15 |
Correct |
61 ms |
77480 KB |
Output is correct |
16 |
Correct |
68 ms |
77388 KB |
Output is correct |
17 |
Correct |
47 ms |
21428 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
1228 KB |
Output is correct |
21 |
Correct |
1 ms |
2132 KB |
Output is correct |
22 |
Correct |
2 ms |
3028 KB |
Output is correct |
23 |
Correct |
5 ms |
468 KB |
Output is correct |
24 |
Correct |
6 ms |
1112 KB |
Output is correct |
25 |
Correct |
9 ms |
5620 KB |
Output is correct |
26 |
Correct |
7 ms |
3668 KB |
Output is correct |
27 |
Correct |
23 ms |
5972 KB |
Output is correct |
28 |
Correct |
65 ms |
46556 KB |
Output is correct |
29 |
Correct |
72 ms |
46828 KB |
Output is correct |
30 |
Correct |
59 ms |
46188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
5640 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
472 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
34 ms |
6100 KB |
Output is correct |
9 |
Correct |
40 ms |
6100 KB |
Output is correct |
10 |
Correct |
23 ms |
5344 KB |
Output is correct |
11 |
Correct |
51 ms |
20284 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
2004 KB |
Output is correct |
16 |
Correct |
2 ms |
3028 KB |
Output is correct |
17 |
Correct |
15 ms |
596 KB |
Output is correct |
18 |
Correct |
16 ms |
1264 KB |
Output is correct |
19 |
Correct |
34 ms |
6092 KB |
Output is correct |
20 |
Correct |
80 ms |
46700 KB |
Output is correct |
21 |
Correct |
53 ms |
21568 KB |
Output is correct |
22 |
Correct |
64 ms |
45692 KB |
Output is correct |
23 |
Correct |
86 ms |
78600 KB |
Output is correct |
24 |
Correct |
45 ms |
29132 KB |
Output is correct |
25 |
Correct |
74 ms |
77516 KB |
Output is correct |
26 |
Correct |
73 ms |
77448 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
2004 KB |
Output is correct |
31 |
Correct |
1 ms |
3028 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
1196 KB |
Output is correct |
34 |
Correct |
4 ms |
5608 KB |
Output is correct |
35 |
Correct |
3 ms |
3668 KB |
Output is correct |
36 |
Correct |
21 ms |
5972 KB |
Output is correct |
37 |
Correct |
37 ms |
21428 KB |
Output is correct |
38 |
Correct |
60 ms |
46560 KB |
Output is correct |
39 |
Correct |
66 ms |
46816 KB |
Output is correct |
40 |
Correct |
54 ms |
46096 KB |
Output is correct |
41 |
Correct |
61 ms |
77480 KB |
Output is correct |
42 |
Correct |
68 ms |
77388 KB |
Output is correct |
43 |
Correct |
47 ms |
21428 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
724 KB |
Output is correct |
46 |
Correct |
1 ms |
1228 KB |
Output is correct |
47 |
Correct |
1 ms |
2132 KB |
Output is correct |
48 |
Correct |
2 ms |
3028 KB |
Output is correct |
49 |
Correct |
5 ms |
468 KB |
Output is correct |
50 |
Correct |
6 ms |
1112 KB |
Output is correct |
51 |
Correct |
9 ms |
5620 KB |
Output is correct |
52 |
Correct |
7 ms |
3668 KB |
Output is correct |
53 |
Correct |
23 ms |
5972 KB |
Output is correct |
54 |
Correct |
65 ms |
46556 KB |
Output is correct |
55 |
Correct |
72 ms |
46828 KB |
Output is correct |
56 |
Correct |
59 ms |
46188 KB |
Output is correct |
57 |
Correct |
117 ms |
82020 KB |
Output is correct |
58 |
Correct |
33 ms |
6144 KB |
Output is correct |
59 |
Correct |
57 ms |
21684 KB |
Output is correct |