#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 5e4+1 , oo = 1e9;
int dist[17][mxN][5];
void minset(int& a, int b) {
a = min(a,b);
}
const array<int,5> init = {oo,oo,oo,oo,oo};
int main() {
int k,n,m,o; cin >> k >> n >> m >> o;
for(int i=0;i<n;++i) {
for(int j=0;j<k;++j) {
dist[0][i][j] = oo;
}
}
for(int i=0;i<m;++i) {
int a,b,t; cin >> a >> b >> t;
dist[0][a][b%k] = t;
}
int B = (n+k-1)/k;
for(int j=1;(1<<j)<B;++j) {
int halflen = 1<<(j-1);
for(int a =0;a<n;++a) {
int block = a/k;
int delta = k*(halflen+block);
for(int c=0;c<k;++c) {
dist[j][a][c] = oo;
for(int b=0;b<k;++b) {
int id = b+delta;
if(id>=n) break;
minset(dist[j][a][c], dist[j-1][a][b]+dist[j-1][id][c]);
}
}
}
}
while(o--) {
int a,b; cin >> a >> b;
int id = a/k;
int d = b/k - id;
if(d<0) {
cout << "-1\n";
continue;
}
array<int,5> at = init,to;
at[a%k]=0;
for(int i=0;1<<i <= d;++i) {
if(d&(1<<i)) {
to = init;
// update at table
for(int c=0;c<k;++c)
for(int e=0;e<k;++e)
minset(to[e], at[c]+dist[i][id*k+c][e]);
swap(to,at);
id+=1<<i;
}
}
cout << (at[b%k]==oo?-1:at[b%k]) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
17004 KB |
Output is correct |
2 |
Correct |
2 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
5 ms |
620 KB |
Output is correct |
6 |
Correct |
5 ms |
620 KB |
Output is correct |
7 |
Correct |
5 ms |
620 KB |
Output is correct |
8 |
Correct |
91 ms |
16876 KB |
Output is correct |
9 |
Correct |
104 ms |
16876 KB |
Output is correct |
10 |
Correct |
49 ms |
16108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
16640 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
30 ms |
652 KB |
Output is correct |
8 |
Correct |
31 ms |
748 KB |
Output is correct |
9 |
Correct |
88 ms |
16876 KB |
Output is correct |
10 |
Correct |
172 ms |
17388 KB |
Output is correct |
11 |
Correct |
130 ms |
16876 KB |
Output is correct |
12 |
Correct |
118 ms |
16364 KB |
Output is correct |
13 |
Correct |
165 ms |
10476 KB |
Output is correct |
14 |
Correct |
97 ms |
10092 KB |
Output is correct |
15 |
Correct |
100 ms |
9324 KB |
Output is correct |
16 |
Correct |
98 ms |
9324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
3 ms |
620 KB |
Output is correct |
8 |
Correct |
6 ms |
748 KB |
Output is correct |
9 |
Correct |
4 ms |
620 KB |
Output is correct |
10 |
Correct |
56 ms |
16748 KB |
Output is correct |
11 |
Correct |
96 ms |
16512 KB |
Output is correct |
12 |
Correct |
138 ms |
17132 KB |
Output is correct |
13 |
Correct |
162 ms |
17516 KB |
Output is correct |
14 |
Correct |
125 ms |
16876 KB |
Output is correct |
15 |
Correct |
90 ms |
9196 KB |
Output is correct |
16 |
Correct |
91 ms |
9196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
3 ms |
620 KB |
Output is correct |
8 |
Correct |
6 ms |
748 KB |
Output is correct |
9 |
Correct |
4 ms |
620 KB |
Output is correct |
10 |
Correct |
56 ms |
16748 KB |
Output is correct |
11 |
Correct |
96 ms |
16512 KB |
Output is correct |
12 |
Correct |
138 ms |
17132 KB |
Output is correct |
13 |
Correct |
162 ms |
17516 KB |
Output is correct |
14 |
Correct |
125 ms |
16876 KB |
Output is correct |
15 |
Correct |
90 ms |
9196 KB |
Output is correct |
16 |
Correct |
91 ms |
9196 KB |
Output is correct |
17 |
Correct |
107 ms |
16620 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
11 ms |
620 KB |
Output is correct |
24 |
Correct |
11 ms |
620 KB |
Output is correct |
25 |
Correct |
13 ms |
620 KB |
Output is correct |
26 |
Correct |
13 ms |
620 KB |
Output is correct |
27 |
Correct |
66 ms |
16876 KB |
Output is correct |
28 |
Correct |
158 ms |
17260 KB |
Output is correct |
29 |
Correct |
163 ms |
17516 KB |
Output is correct |
30 |
Correct |
134 ms |
17004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
17004 KB |
Output is correct |
2 |
Correct |
2 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
5 ms |
620 KB |
Output is correct |
6 |
Correct |
5 ms |
620 KB |
Output is correct |
7 |
Correct |
5 ms |
620 KB |
Output is correct |
8 |
Correct |
91 ms |
16876 KB |
Output is correct |
9 |
Correct |
104 ms |
16876 KB |
Output is correct |
10 |
Correct |
49 ms |
16108 KB |
Output is correct |
11 |
Correct |
129 ms |
16640 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
30 ms |
652 KB |
Output is correct |
18 |
Correct |
31 ms |
748 KB |
Output is correct |
19 |
Correct |
88 ms |
16876 KB |
Output is correct |
20 |
Correct |
172 ms |
17388 KB |
Output is correct |
21 |
Correct |
130 ms |
16876 KB |
Output is correct |
22 |
Correct |
118 ms |
16364 KB |
Output is correct |
23 |
Correct |
165 ms |
10476 KB |
Output is correct |
24 |
Correct |
97 ms |
10092 KB |
Output is correct |
25 |
Correct |
100 ms |
9324 KB |
Output is correct |
26 |
Correct |
98 ms |
9324 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
2 ms |
620 KB |
Output is correct |
33 |
Correct |
3 ms |
620 KB |
Output is correct |
34 |
Correct |
6 ms |
748 KB |
Output is correct |
35 |
Correct |
4 ms |
620 KB |
Output is correct |
36 |
Correct |
56 ms |
16748 KB |
Output is correct |
37 |
Correct |
96 ms |
16512 KB |
Output is correct |
38 |
Correct |
138 ms |
17132 KB |
Output is correct |
39 |
Correct |
162 ms |
17516 KB |
Output is correct |
40 |
Correct |
125 ms |
16876 KB |
Output is correct |
41 |
Correct |
90 ms |
9196 KB |
Output is correct |
42 |
Correct |
91 ms |
9196 KB |
Output is correct |
43 |
Correct |
107 ms |
16620 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
11 ms |
620 KB |
Output is correct |
50 |
Correct |
11 ms |
620 KB |
Output is correct |
51 |
Correct |
13 ms |
620 KB |
Output is correct |
52 |
Correct |
13 ms |
620 KB |
Output is correct |
53 |
Correct |
66 ms |
16876 KB |
Output is correct |
54 |
Correct |
158 ms |
17260 KB |
Output is correct |
55 |
Correct |
163 ms |
17516 KB |
Output is correct |
56 |
Correct |
134 ms |
17004 KB |
Output is correct |
57 |
Correct |
241 ms |
17516 KB |
Output is correct |
58 |
Correct |
94 ms |
16876 KB |
Output is correct |
59 |
Correct |
139 ms |
16956 KB |
Output is correct |