답안 #383724

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
383724 2021-03-30T17:31:05 Z ntabc05101 Toll (BOI17_toll) C++14
100 / 100
171 ms 87020 KB
#include<bits/stdc++.h>
using namespace std;

const int inf = 1e9 + 7;
const int mx = 50000;
const int log_mx = 17;

int k, n, m, o;
int c[5 + mx][log_mx][5][5], result[5][5], tmp[5][5];

void combine(int target[5][5], int a[5][5], int b[5][5]) {
        for (int i = 0; i < k; i++) {
                for (int j = 0; j < k; j++) {
                        for (int l = 0; l < k; l++) {
                                target[i][j] = min(target[i][j], a[i][l] + b[l][j]);
                        }
                }
        }
}

#define taskname "BOI17_toll"

int main() {
        if (fopen(taskname".in", "r")) {
                freopen(taskname".in", "r", stdin);
                freopen(taskname".out", "w", stdout);
        }

        ios_base::sync_with_stdio(0); cin.tie(0);

        cin >> k >> n >> m >> o;
        for (int i = 0; i < n; i++) {
                for (int j = 0; j < log_mx; j++) {
                        for (int m = 0; m < k; m++) {
                                for (int l = 0; l < k; l++) {
                                        c[i][j][m][l] = inf;
                                }
                        }
                }
        }
        for (int i = 0; i < m; i++) {
                int a, b, w; cin >> a >> b >> w;
                c[a / k][0][a % k][b % k] = w;
        }

        for (int j = 1; j < log_mx; j++) {
                for (int i = 0; i + (1 << j - 1) < (n + k - 1) / k; i++) {
                        combine(c[i][j], c[i][j - 1], c[i + (1 << j - 1)][j - 1]);
                }
        }

        /*for (int i = 0; i < n; i++) {
                for (int j = 0; j < log_mx; j++) {
                        for (int m = 0; m < k; m++) {
                                for (int l = 0; l < k; l++) {
                                        cout << i << " " << j << " " << m << " " << l << " " << c[i][j][m][l] << "\n";
                                }
                        }
                }
        }*/

        while (o--) {
                int a, b; cin >> a >> b;

                for (int i = 0; i < k; i++) {
                        for (int j = 0; j < k; j++) {
                                result[i][j] = inf;
                        }
                }
                for (int i = 0; i < k; i++) result[i][i] = 0;
                for (int curr = a / k, dest = b / k, i = log_mx - 1; i >= 0; i--) {
                        if (curr + (1 << i) <= dest) {
                                for (int j = 0; j < k; j++) {
                                        for (int l = 0; l < k; l++) {
                                                tmp[j][l] = inf;
                                        }
                                }
                                combine(tmp, result, c[curr][i]);
                                for (int j = 0; j < k; j++) {
                                        for (int l = 0; l < k; l++) {
                                                result[j][l] = tmp[j][l];
                                        }
                                }
                                curr += (1 << i);
                        }
                }

                cout << (result[a % k][b % k] == inf ? -1: result[a % k][b % k]) << "\n";
        }

        return 0;
}

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:47:45: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   47 |                 for (int i = 0; i + (1 << j - 1) < (n + k - 1) / k; i++) {
      |                                           ~~^~~
toll.cpp:48:69: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   48 |                         combine(c[i][j], c[i][j - 1], c[i + (1 << j - 1)][j - 1]);
      |                                                                   ~~^~~
toll.cpp:25:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   25 |                 freopen(taskname".in", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:26:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   26 |                 freopen(taskname".out", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 84588 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 384 KB Output is correct
5 Correct 3 ms 2156 KB Output is correct
6 Correct 3 ms 2028 KB Output is correct
7 Correct 2 ms 2028 KB Output is correct
8 Correct 99 ms 84388 KB Output is correct
9 Correct 100 ms 84460 KB Output is correct
10 Correct 79 ms 83692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 85228 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 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 6 ms 2156 KB Output is correct
8 Correct 7 ms 2156 KB Output is correct
9 Correct 95 ms 84588 KB Output is correct
10 Correct 129 ms 85868 KB Output is correct
11 Correct 121 ms 85356 KB Output is correct
12 Correct 119 ms 84844 KB Output is correct
13 Correct 103 ms 52692 KB Output is correct
14 Correct 74 ms 51692 KB Output is correct
15 Correct 80 ms 51564 KB Output is correct
16 Correct 80 ms 51436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 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 3 ms 2028 KB Output is correct
7 Correct 3 ms 2028 KB Output is correct
8 Correct 4 ms 2028 KB Output is correct
9 Correct 3 ms 2028 KB Output is correct
10 Correct 87 ms 84332 KB Output is correct
11 Correct 118 ms 85100 KB Output is correct
12 Correct 130 ms 85808 KB Output is correct
13 Correct 126 ms 85868 KB Output is correct
14 Correct 115 ms 85344 KB Output is correct
15 Correct 76 ms 51436 KB Output is correct
16 Correct 73 ms 51436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 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 3 ms 2028 KB Output is correct
7 Correct 3 ms 2028 KB Output is correct
8 Correct 4 ms 2028 KB Output is correct
9 Correct 3 ms 2028 KB Output is correct
10 Correct 87 ms 84332 KB Output is correct
11 Correct 118 ms 85100 KB Output is correct
12 Correct 130 ms 85808 KB Output is correct
13 Correct 126 ms 85868 KB Output is correct
14 Correct 115 ms 85344 KB Output is correct
15 Correct 76 ms 51436 KB Output is correct
16 Correct 73 ms 51436 KB Output is correct
17 Correct 116 ms 85228 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 3 ms 2028 KB Output is correct
24 Correct 4 ms 2156 KB Output is correct
25 Correct 7 ms 2156 KB Output is correct
26 Correct 6 ms 2156 KB Output is correct
27 Correct 90 ms 84424 KB Output is correct
28 Correct 128 ms 85740 KB Output is correct
29 Correct 137 ms 85996 KB Output is correct
30 Correct 118 ms 85356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 84588 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 384 KB Output is correct
5 Correct 3 ms 2156 KB Output is correct
6 Correct 3 ms 2028 KB Output is correct
7 Correct 2 ms 2028 KB Output is correct
8 Correct 99 ms 84388 KB Output is correct
9 Correct 100 ms 84460 KB Output is correct
10 Correct 79 ms 83692 KB Output is correct
11 Correct 117 ms 85228 KB Output is correct
12 Correct 2 ms 364 KB Output is correct
13 Correct 2 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 6 ms 2156 KB Output is correct
18 Correct 7 ms 2156 KB Output is correct
19 Correct 95 ms 84588 KB Output is correct
20 Correct 129 ms 85868 KB Output is correct
21 Correct 121 ms 85356 KB Output is correct
22 Correct 119 ms 84844 KB Output is correct
23 Correct 103 ms 52692 KB Output is correct
24 Correct 74 ms 51692 KB Output is correct
25 Correct 80 ms 51564 KB Output is correct
26 Correct 80 ms 51436 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 384 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 3 ms 2028 KB Output is correct
33 Correct 3 ms 2028 KB Output is correct
34 Correct 4 ms 2028 KB Output is correct
35 Correct 3 ms 2028 KB Output is correct
36 Correct 87 ms 84332 KB Output is correct
37 Correct 118 ms 85100 KB Output is correct
38 Correct 130 ms 85808 KB Output is correct
39 Correct 126 ms 85868 KB Output is correct
40 Correct 115 ms 85344 KB Output is correct
41 Correct 76 ms 51436 KB Output is correct
42 Correct 73 ms 51436 KB Output is correct
43 Correct 116 ms 85228 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 3 ms 2028 KB Output is correct
50 Correct 4 ms 2156 KB Output is correct
51 Correct 7 ms 2156 KB Output is correct
52 Correct 6 ms 2156 KB Output is correct
53 Correct 90 ms 84424 KB Output is correct
54 Correct 128 ms 85740 KB Output is correct
55 Correct 137 ms 85996 KB Output is correct
56 Correct 118 ms 85356 KB Output is correct
57 Correct 171 ms 87020 KB Output is correct
58 Correct 97 ms 84332 KB Output is correct
59 Correct 128 ms 85304 KB Output is correct