Submission #530006

# Submission time Handle Problem Language Result Execution time Memory
530006 2022-02-24T11:17:14 Z Alex_tz307 Toll (BOI17_toll) C++17
100 / 100
103 ms 79540 KB
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

const int kN = 5e4;
const int kLog = 15;
const int kK = 5;
int res[kK][kK], ret[kK][kK], dp[kN][1 + kLog][kK][kK];

void minSelf(int &x, int y) {
  if (y < x) {
    x = y;
  }
}

void combine(int n, int a[kK][kK], int b[kK][kK], int c[kK][kK]) {
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        minSelf(a[i][j], b[i][k] + c[k][j]);
      }
    }
  }
}

void testCase() {
  int k, n, m, q;
  cin >> k >> n >> m >> q;
  for (int i = 0; i <= (n - 1) / k; ++i) {
    for (int j = 0; (1 << j) <= (n - 1) / k; ++j) {
      for (int a = 0; a < k; ++a) {
        for (int b = 0; b < k; ++b) {
          dp[i][j][a][b] = INF;
        }
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    int a, b, t;
    cin >> a >> b >> t;
    dp[a / k][0][a % k][b % k] = t;
  }
  for (int j = 1; (1 << j) <= (n - 1) / k; ++j) {
    for (int i = 0; i + (1 << j) - 1 <= (n - 1) / k; ++i) {
      combine(k, dp[i][j], dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
    }
  }
  for (int qq = 0; qq < q; ++qq) {
    int a, b;
    cin >> a >> b;
    if (a / k == b / k) {
      cout << "-1\n";
      continue;
    }
    for (int i = 0; i < k; ++i) {
      for (int j = 0; j < k; ++j) {
        if (i == j) {
          res[i][i] = 0;
        } else {
          res[i][j] = INF;
        }
      }
    }
    int s = a / k, t = b / k;
    for (int i = kLog; i >= 0; --i) {
      if (s + (1 << i) <= t) {
        for (int l = 0; l < k; ++l) {
          for (int c = 0; c < k; ++c) {
            ret[l][c] = res[l][c];
            res[l][c] = INF;
          }
        }
        combine(k, res, ret, dp[s][i]);
        s += (1 << i);
      }
    }
    if (res[a % k][b % k] == INF) {
      cout << "-1\n";
    } else {
      cout << res[a % k][b % k] << '\n';
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int tests = 1;
  for (int tc = 0; tc < tests; ++tc) {
    testCase();
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 69 ms 79540 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 3 ms 1868 KB Output is correct
6 Correct 2 ms 1872 KB Output is correct
7 Correct 2 ms 1880 KB Output is correct
8 Correct 69 ms 79420 KB Output is correct
9 Correct 64 ms 79372 KB Output is correct
10 Correct 52 ms 78620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 41044 KB Output is correct
2 Correct 0 ms 324 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 5 ms 1996 KB Output is correct
8 Correct 6 ms 1228 KB Output is correct
9 Correct 64 ms 79480 KB Output is correct
10 Correct 81 ms 28740 KB Output is correct
11 Correct 75 ms 41212 KB Output is correct
12 Correct 73 ms 27812 KB Output is correct
13 Correct 67 ms 12084 KB Output is correct
14 Correct 43 ms 17404 KB Output is correct
15 Correct 49 ms 10908 KB Output is correct
16 Correct 46 ms 10828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 320 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 324 KB Output is correct
6 Correct 2 ms 1868 KB Output is correct
7 Correct 1 ms 1100 KB Output is correct
8 Correct 3 ms 592 KB Output is correct
9 Correct 2 ms 716 KB Output is correct
10 Correct 60 ms 79340 KB Output is correct
11 Correct 62 ms 40936 KB Output is correct
12 Correct 76 ms 28556 KB Output is correct
13 Correct 81 ms 28732 KB Output is correct
14 Correct 66 ms 28208 KB Output is correct
15 Correct 43 ms 10884 KB Output is correct
16 Correct 43 ms 10848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 320 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 324 KB Output is correct
6 Correct 2 ms 1868 KB Output is correct
7 Correct 1 ms 1100 KB Output is correct
8 Correct 3 ms 592 KB Output is correct
9 Correct 2 ms 716 KB Output is correct
10 Correct 60 ms 79340 KB Output is correct
11 Correct 62 ms 40936 KB Output is correct
12 Correct 76 ms 28556 KB Output is correct
13 Correct 81 ms 28732 KB Output is correct
14 Correct 66 ms 28208 KB Output is correct
15 Correct 43 ms 10884 KB Output is correct
16 Correct 43 ms 10848 KB Output is correct
17 Correct 85 ms 40996 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 328 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 2 ms 1880 KB Output is correct
24 Correct 3 ms 1100 KB Output is correct
25 Correct 6 ms 716 KB Output is correct
26 Correct 4 ms 780 KB Output is correct
27 Correct 68 ms 79396 KB Output is correct
28 Correct 96 ms 28564 KB Output is correct
29 Correct 103 ms 28840 KB Output is correct
30 Correct 68 ms 28184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 79540 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 3 ms 1868 KB Output is correct
6 Correct 2 ms 1872 KB Output is correct
7 Correct 2 ms 1880 KB Output is correct
8 Correct 69 ms 79420 KB Output is correct
9 Correct 64 ms 79372 KB Output is correct
10 Correct 52 ms 78620 KB Output is correct
11 Correct 69 ms 41044 KB Output is correct
12 Correct 0 ms 324 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 5 ms 1996 KB Output is correct
18 Correct 6 ms 1228 KB Output is correct
19 Correct 64 ms 79480 KB Output is correct
20 Correct 81 ms 28740 KB Output is correct
21 Correct 75 ms 41212 KB Output is correct
22 Correct 73 ms 27812 KB Output is correct
23 Correct 67 ms 12084 KB Output is correct
24 Correct 43 ms 17404 KB Output is correct
25 Correct 49 ms 10908 KB Output is correct
26 Correct 46 ms 10828 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 320 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 0 ms 324 KB Output is correct
32 Correct 2 ms 1868 KB Output is correct
33 Correct 1 ms 1100 KB Output is correct
34 Correct 3 ms 592 KB Output is correct
35 Correct 2 ms 716 KB Output is correct
36 Correct 60 ms 79340 KB Output is correct
37 Correct 62 ms 40936 KB Output is correct
38 Correct 76 ms 28556 KB Output is correct
39 Correct 81 ms 28732 KB Output is correct
40 Correct 66 ms 28208 KB Output is correct
41 Correct 43 ms 10884 KB Output is correct
42 Correct 43 ms 10848 KB Output is correct
43 Correct 85 ms 40996 KB Output is correct
44 Correct 1 ms 308 KB Output is correct
45 Correct 1 ms 324 KB Output is correct
46 Correct 0 ms 204 KB Output is correct
47 Correct 1 ms 328 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 2 ms 1880 KB Output is correct
50 Correct 3 ms 1100 KB Output is correct
51 Correct 6 ms 716 KB Output is correct
52 Correct 4 ms 780 KB Output is correct
53 Correct 68 ms 79396 KB Output is correct
54 Correct 96 ms 28564 KB Output is correct
55 Correct 103 ms 28840 KB Output is correct
56 Correct 68 ms 28184 KB Output is correct
57 Correct 103 ms 23044 KB Output is correct
58 Correct 66 ms 79416 KB Output is correct
59 Correct 82 ms 41156 KB Output is correct