#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e4*5;
int dp[mxN][16][5];
int nidx[mxN][16][5];
int n,k;
int query(int g, int b, int vals[]){
int i = 0;
for (i = 0; i < 16; i++){
if (nidx[g*k][i][0] == -1 || nidx[g*k][i][0] > b){
break;
}
}
if (i == 0){
return vals[b % k];
}
i -= 1;
int nvals[k];
for (int j =0; j <k; j++){
nvals[j] = INT_MAX;
}
for (int j =0; j < k; j++){
if (vals[j] == INT_MAX){
continue;
}
for (int l = 0; l < k; l++){
if (dp[g*k+j][i][l] == INT_MAX){
continue;
}
nvals[l] = min(nvals[l], dp[g*k+j][i][l]+vals[j]);
}
}
return query(nidx[g*k][i][0] / k, b, nvals);
}
int main()
{
memset(nidx,-1,sizeof(nidx));
for (int i =0; i < mxN; i++){
for (int j = 0; j < 16; j++){
for (int l = 0; l < 5; l++){
dp[i][j][l] = INT_MAX;
}
}
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int m,o;
cin >> k >> n >> m >> o;
for (int i =n-1; i >= 0; i--){
int g = i/k;
if (g == (n-1)/k){
continue;
}
// cout << i << " " << g << '\n';
for (int j = 0; j < k; j++){
if ((g+1)*k+j >= n){
break;
}
nidx[i][0][j] = (g+1)*k+j;
}
for (int j = 1; j < 16; j++){
for (int l = 0; l < k; l++){
if (nidx[i][j-1][0] == -1){
break;
}
nidx[i][j][l] = nidx[nidx[i][j-1][0]][j-1][l];
}
}
}
vector<vector<pair<int,int>>> connect(n);
int a,b,t;
for (int i =0; i < m; i++){
cin >> a >> b >> t;
connect[a].push_back({b,t});
}
/* for (int i =0; i < 4*1e8; i++){
continue;
}*/
// cout << "LEELL\n";
/* for (int i =0; i < n; i++){
cout << i << ": \n";
for (int j = 0; j < 5; j++){
for (int l = 0; l < k; l++){
cout << dp[i][j][l] << " ";
}
cout << '\n';
}
cout << '\n';
}*/
for (int i = n-1; i >= 0; i--){
for (int j =0; j < connect[i].size(); j++){
int curr = connect[i][j].first;
dp[i][0][curr % k] = connect[i][j].second;
// nidx[i][0][curr%k] = curr;
}
bool has;
for (int l = 1; l < 16; l++){
/* if (nidx[a][l-1][0] == -1){
break;
}*/
has = false;
for (int p1 = 0; p1 < k; p1++){
for (int p2 = 0; p2 < k; p2++){
if (dp[i][l-1][p2] == INT_MAX){
continue;
}
has = true;
// cout << (long long int) dp[nidx[i][l-1][p2]][l-1][p1] + dp[i][l-1][p2] << '\n';
if ((long long int) dp[nidx[i][l-1][p2]][l-1][p1] + dp[i][l-1][p2] < (long long int) dp[i][l][p1]){
dp[i][l][p1] = dp[nidx[i][l-1][p2]][l-1][p1] + dp[i][l-1][p2];
// nidx[i][l][p1] = nidx[nidx[i][l-1][p2]][l-1][p1];
}
// dp[i][l][p1] = min(dp[i][l][p1],dp[nidx[i][l-1][p2]][l-1][p1] + dp[i][l-1][p2]);
}
if (!has){
break;
}
}
if (!has){
break;
}
}
}
/* for (int i =0; i < n; i++){
cout << i << ": \n";
for (int j = 0; j < 5; j++){
for (int l = 0; l < k; l++){
if (dp[i][j][l] == INT_MAX){
cout << "0 ";
} else {
cout << dp[i][j][l] << " ";
}
}
cout << '\n';
}
cout << '\n';
}
cout << '\n';
for (int i =0; i < n; i++){
cout << i << ": \n";
for (int j = 0; j < 5; j++){
for (int l = 0; l < k; l++){
cout << nidx[i][j][l] << " ";
}
cout << '\n';
}
cout << '\n';
}*/
//return 0;
int temp[k];
for (int i =0; i < o; i++){
cin >> a >> b;
for (int j =0; j < k; j++){
temp[j] = INT_MAX;
}
temp[a%k] = 0;
int res = query(a/k,b,temp);
if (res == INT_MAX){
cout << "-1\n";
} else {
cout << res << '\n';
}
}
return 0;
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:106:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for (int j =0; j < connect[i].size(); j++){
| ~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
34904 KB |
Output is correct |
2 |
Correct |
13 ms |
31600 KB |
Output is correct |
3 |
Correct |
13 ms |
31604 KB |
Output is correct |
4 |
Correct |
12 ms |
31624 KB |
Output is correct |
5 |
Correct |
13 ms |
31688 KB |
Output is correct |
6 |
Correct |
14 ms |
31708 KB |
Output is correct |
7 |
Correct |
14 ms |
31688 KB |
Output is correct |
8 |
Correct |
48 ms |
34892 KB |
Output is correct |
9 |
Correct |
47 ms |
34764 KB |
Output is correct |
10 |
Correct |
29 ms |
32968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
34836 KB |
Output is correct |
2 |
Correct |
13 ms |
31524 KB |
Output is correct |
3 |
Correct |
13 ms |
31572 KB |
Output is correct |
4 |
Correct |
13 ms |
31592 KB |
Output is correct |
5 |
Correct |
12 ms |
31636 KB |
Output is correct |
6 |
Correct |
14 ms |
31628 KB |
Output is correct |
7 |
Correct |
16 ms |
31692 KB |
Output is correct |
8 |
Correct |
17 ms |
31828 KB |
Output is correct |
9 |
Correct |
44 ms |
34832 KB |
Output is correct |
10 |
Correct |
88 ms |
35836 KB |
Output is correct |
11 |
Correct |
70 ms |
34932 KB |
Output is correct |
12 |
Correct |
57 ms |
34752 KB |
Output is correct |
13 |
Correct |
95 ms |
35660 KB |
Output is correct |
14 |
Correct |
58 ms |
35452 KB |
Output is correct |
15 |
Correct |
68 ms |
34852 KB |
Output is correct |
16 |
Correct |
68 ms |
34832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31572 KB |
Output is correct |
2 |
Correct |
13 ms |
31572 KB |
Output is correct |
3 |
Correct |
12 ms |
31632 KB |
Output is correct |
4 |
Correct |
12 ms |
31572 KB |
Output is correct |
5 |
Correct |
12 ms |
31520 KB |
Output is correct |
6 |
Correct |
13 ms |
31620 KB |
Output is correct |
7 |
Correct |
13 ms |
31688 KB |
Output is correct |
8 |
Correct |
16 ms |
31768 KB |
Output is correct |
9 |
Correct |
14 ms |
31648 KB |
Output is correct |
10 |
Correct |
39 ms |
34764 KB |
Output is correct |
11 |
Correct |
63 ms |
34832 KB |
Output is correct |
12 |
Correct |
88 ms |
35776 KB |
Output is correct |
13 |
Correct |
92 ms |
35984 KB |
Output is correct |
14 |
Correct |
81 ms |
35296 KB |
Output is correct |
15 |
Correct |
85 ms |
34028 KB |
Output is correct |
16 |
Correct |
69 ms |
34056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31572 KB |
Output is correct |
2 |
Correct |
13 ms |
31572 KB |
Output is correct |
3 |
Correct |
12 ms |
31632 KB |
Output is correct |
4 |
Correct |
12 ms |
31572 KB |
Output is correct |
5 |
Correct |
12 ms |
31520 KB |
Output is correct |
6 |
Correct |
13 ms |
31620 KB |
Output is correct |
7 |
Correct |
13 ms |
31688 KB |
Output is correct |
8 |
Correct |
16 ms |
31768 KB |
Output is correct |
9 |
Correct |
14 ms |
31648 KB |
Output is correct |
10 |
Correct |
39 ms |
34764 KB |
Output is correct |
11 |
Correct |
63 ms |
34832 KB |
Output is correct |
12 |
Correct |
88 ms |
35776 KB |
Output is correct |
13 |
Correct |
92 ms |
35984 KB |
Output is correct |
14 |
Correct |
81 ms |
35296 KB |
Output is correct |
15 |
Correct |
85 ms |
34028 KB |
Output is correct |
16 |
Correct |
69 ms |
34056 KB |
Output is correct |
17 |
Correct |
71 ms |
34876 KB |
Output is correct |
18 |
Correct |
13 ms |
31540 KB |
Output is correct |
19 |
Correct |
13 ms |
31632 KB |
Output is correct |
20 |
Correct |
13 ms |
31572 KB |
Output is correct |
21 |
Correct |
13 ms |
31628 KB |
Output is correct |
22 |
Correct |
13 ms |
31576 KB |
Output is correct |
23 |
Correct |
14 ms |
31700 KB |
Output is correct |
24 |
Correct |
15 ms |
31700 KB |
Output is correct |
25 |
Correct |
16 ms |
31776 KB |
Output is correct |
26 |
Correct |
16 ms |
31728 KB |
Output is correct |
27 |
Correct |
41 ms |
34844 KB |
Output is correct |
28 |
Correct |
93 ms |
35788 KB |
Output is correct |
29 |
Correct |
96 ms |
35988 KB |
Output is correct |
30 |
Correct |
83 ms |
35320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
34904 KB |
Output is correct |
2 |
Correct |
13 ms |
31600 KB |
Output is correct |
3 |
Correct |
13 ms |
31604 KB |
Output is correct |
4 |
Correct |
12 ms |
31624 KB |
Output is correct |
5 |
Correct |
13 ms |
31688 KB |
Output is correct |
6 |
Correct |
14 ms |
31708 KB |
Output is correct |
7 |
Correct |
14 ms |
31688 KB |
Output is correct |
8 |
Correct |
48 ms |
34892 KB |
Output is correct |
9 |
Correct |
47 ms |
34764 KB |
Output is correct |
10 |
Correct |
29 ms |
32968 KB |
Output is correct |
11 |
Correct |
66 ms |
34836 KB |
Output is correct |
12 |
Correct |
13 ms |
31524 KB |
Output is correct |
13 |
Correct |
13 ms |
31572 KB |
Output is correct |
14 |
Correct |
13 ms |
31592 KB |
Output is correct |
15 |
Correct |
12 ms |
31636 KB |
Output is correct |
16 |
Correct |
14 ms |
31628 KB |
Output is correct |
17 |
Correct |
16 ms |
31692 KB |
Output is correct |
18 |
Correct |
17 ms |
31828 KB |
Output is correct |
19 |
Correct |
44 ms |
34832 KB |
Output is correct |
20 |
Correct |
88 ms |
35836 KB |
Output is correct |
21 |
Correct |
70 ms |
34932 KB |
Output is correct |
22 |
Correct |
57 ms |
34752 KB |
Output is correct |
23 |
Correct |
95 ms |
35660 KB |
Output is correct |
24 |
Correct |
58 ms |
35452 KB |
Output is correct |
25 |
Correct |
68 ms |
34852 KB |
Output is correct |
26 |
Correct |
68 ms |
34832 KB |
Output is correct |
27 |
Correct |
12 ms |
31572 KB |
Output is correct |
28 |
Correct |
13 ms |
31572 KB |
Output is correct |
29 |
Correct |
12 ms |
31632 KB |
Output is correct |
30 |
Correct |
12 ms |
31572 KB |
Output is correct |
31 |
Correct |
12 ms |
31520 KB |
Output is correct |
32 |
Correct |
13 ms |
31620 KB |
Output is correct |
33 |
Correct |
13 ms |
31688 KB |
Output is correct |
34 |
Correct |
16 ms |
31768 KB |
Output is correct |
35 |
Correct |
14 ms |
31648 KB |
Output is correct |
36 |
Correct |
39 ms |
34764 KB |
Output is correct |
37 |
Correct |
63 ms |
34832 KB |
Output is correct |
38 |
Correct |
88 ms |
35776 KB |
Output is correct |
39 |
Correct |
92 ms |
35984 KB |
Output is correct |
40 |
Correct |
81 ms |
35296 KB |
Output is correct |
41 |
Correct |
85 ms |
34028 KB |
Output is correct |
42 |
Correct |
69 ms |
34056 KB |
Output is correct |
43 |
Correct |
71 ms |
34876 KB |
Output is correct |
44 |
Correct |
13 ms |
31540 KB |
Output is correct |
45 |
Correct |
13 ms |
31632 KB |
Output is correct |
46 |
Correct |
13 ms |
31572 KB |
Output is correct |
47 |
Correct |
13 ms |
31628 KB |
Output is correct |
48 |
Correct |
13 ms |
31576 KB |
Output is correct |
49 |
Correct |
14 ms |
31700 KB |
Output is correct |
50 |
Correct |
15 ms |
31700 KB |
Output is correct |
51 |
Correct |
16 ms |
31776 KB |
Output is correct |
52 |
Correct |
16 ms |
31728 KB |
Output is correct |
53 |
Correct |
41 ms |
34844 KB |
Output is correct |
54 |
Correct |
93 ms |
35788 KB |
Output is correct |
55 |
Correct |
96 ms |
35988 KB |
Output is correct |
56 |
Correct |
83 ms |
35320 KB |
Output is correct |
57 |
Correct |
121 ms |
38860 KB |
Output is correct |
58 |
Correct |
50 ms |
35196 KB |
Output is correct |
59 |
Correct |
80 ms |
36124 KB |
Output is correct |