Submission #383724

#TimeUsernameProblemLanguageResultExecution timeMemory
383724ntabc05101Toll (BOI17_toll)C++14
100 / 100
171 ms87020 KiB
#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 (stderr)

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);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...