Submission #185488

# Submission time Handle Problem Language Result Execution time Memory
185488 2020-01-11T22:31:27 Z AlexPop28 Toll (BOI17_toll) C++11
0 / 100
3000 ms 47204 KB
#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<

using namespace std;

const int INF = (int)1e9 + 5;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int k, n, m, q; cin >> k >> n >> m >> q;
  int groups = (n + k - 1) / k;
  int log_groups = 32 - __builtin_clz(groups);
  vector<vector<vector<int>>> dist(log_groups,
    vector<vector<int>>(n,
      vector<int>(k, INF)));
  vector<vector<pair<int, int>>> adj(n);
  for (int i = 0; i < m; ++i) {
    int a, b, c; cin >> a >> b >> c;
    adj[a].emplace_back(b, c);
    adj[b].emplace_back(a, c);
    dist[0][a][b % k] = c;
  }
  if ((n - 1) / k >= 2) {
    for (int from = 0; from / k + 2 <= (n - 1) / k; ++from) {
      int st = from / k * k + k;
      int lim = from / k * k + 2 * k;
      if (lim >= n) lim = (n - 1) % k + 1;
      else lim = k;
      for (int to = 0; to < lim; ++to) {
        for (int x = 0; x < k; ++x) {
//          dbg() name(from) name(to + k + st) name(x + st) endl;
          dist[1][from][to] = min(dist[1][from][to],
            dist[0][from][x] + dist[0][x + st][to]);
        }
      }
    }
  }
  for (int p = 2; p < log_groups; ++p) {
    for (int from = 0; from / k + (1 << p) <= (n - 1) / k; ++from) {
      int lim = from / k + (1 << p);
      lim *= k;
      if (lim >= n) lim = (n - 1) % k + 1;
      else lim = k;
      for (int to = 0; to < lim; ++to) {
        int gr = from / k + (1 << (p - 1)); gr *= k;
        for (int x = gr; x < gr + k; ++x) {
          for (int y = gr + k; y < gr + 2 * k; ++y) {
            dist[p][from][to] = min(dist[p][from][to],
              dist[p - 1][from][x - gr] +
                dist[0][x][y - gr - k] +
                  dist[p - 1][y][to]);
          }
        }
      }
    }
  }

  function<int(int, int, int)> GetCost = [&](int from, int to, int max_bit) {
    int gr_from = from / k, gr_to = to / k;
    if (gr_from + 1 == gr_to) {
//      dbg() name(from) name(to) name(dist[0][from][to % k]) endl;
      return dist[0][from][to % k];
    }
    int ans = INF;
    for (int bit = max_bit - 1; bit >= 0; --bit) {
      if ((gr_from + (1 << bit)) > n / k) continue;
      int st = (gr_from + (1 << bit)) * k;
      int lim = st < n ? k : (n - 1) % k + 1;
      for (int x = 0; x < lim; ++x) {
        int curr = dist[bit][from][x];
        if (curr == INF) continue;
        curr += GetCost(x + st, to, bit - 1);
//        dbg() name(from) name(x + st) name(dist[bit][from][x]) name(bit) name(curr) endl;
        ans = min(ans, curr);
      }
    }
    return ans;
  };
  
  while (q--) {
    int a, b; cin >> a >> b;
    int ans = GetCost(a, b, log_groups);
    if (ans == INF) ans = -1;
    cout << ans << '\n';
  }
}
# Verdict Execution time Memory Grader output
1 Incorrect 2553 ms 47204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3080 ms 45540 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Incorrect 4 ms 1016 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Incorrect 4 ms 1016 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2553 ms 47204 KB Output isn't correct
2 Halted 0 ms 0 KB -