Submission #851035

# Submission time Handle Problem Language Result Execution time Memory
851035 2023-09-18T08:59:05 Z anhphant Toll (BOI17_toll) C++14
100 / 100
1213 ms 399232 KB
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>

using namespace std;

const int N = 50000;
const int inf = 1234567890;
const int C = 2000;
const int Q = 10000;

int dp[N], inQue[N];
int preCompute[C][N];
vector<array<int, 2>> adj[N];
array<int, 2> ques[Q];
int occ[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int k, n, m, q;
  cin >> k >> n >> m >> q;

  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    int t;
    cin >> t;
    adj[v].push_back({u, t});
  }

  for (int i = 0; i < n; i++) {
    dp[i] = inf;
    inQue[i] = -1;
    occ[i] = 0;
  }

  vector<int> all;

  for (int i = 0; i < q; i++) {
    cin >> ques[i][0] >> ques[i][1];
    all.push_back(ques[i][0]);
    ++occ[ques[i][0]];
  }

  sort(all.begin(), all.end());
  all.erase(unique(all.begin(), all.end()), all.end());

  sort(all.begin(), all.end(), [&](int i, int j) {
    return occ[i] > occ[j];
  });

  vector<int> pts;
  for (int i = 0; i < min((int) all.size(), C); i++) {
    pts.push_back(all[i]);
    inQue[all[i]] = i;
  }

  for (int i = 0; i < (int) pts.size(); i++) {
    int u = pts[i];
    int b0 = u / k;
    for (int j = b0 * k; j < n; j++) {
      preCompute[i][j] = inf;
    }
    preCompute[i][u] = 0;
    for (int it = (b0 + 1) * k; it < n; it++) {
      for (auto x : adj[it]) {
        preCompute[i][it] = min(preCompute[i][it], preCompute[i][x[0]] + x[1]);
      }
    }
  }

  for (int it = 0; it < q; it++) {
    int u = ques[it][0];
    int v = ques[it][1];
    int b0 = u / k;
    int b1 = v / k;
    if (b0 == b1) {
      cout << -1 << '\n';
    } else {
      int id = inQue[u];
      if (id == -1) {
        dp[u] = 0;
        for (int i = (b0 + 1) * k; i <= v; i++) {
          for (auto x : adj[i]) {
            dp[i] = min(dp[i], dp[x[0]] + x[1]);
          }
        }
        if (dp[v] < inf) {
          cout << dp[v] << '\n';
        } else {
          cout << -1 << '\n';
        }
        dp[u] = inf;
        for (int i = (b0 + 1) * k; i <= v; i++) {
          dp[i] = inf;
        }
      } else {
        if (preCompute[id][v] < inf) {
          cout << preCompute[id][v] << '\n';
        } else {
          cout << -1 << '\n';
        }
      }
    }
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 722 ms 354112 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 13 ms 117336 KB Output is correct
6 Correct 13 ms 115288 KB Output is correct
7 Correct 13 ms 113372 KB Output is correct
8 Correct 721 ms 384340 KB Output is correct
9 Correct 688 ms 395740 KB Output is correct
10 Correct 253 ms 393704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 6232 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 2 ms 2648 KB Output is correct
8 Correct 3 ms 2648 KB Output is correct
9 Correct 15 ms 5620 KB Output is correct
10 Correct 32 ms 7760 KB Output is correct
11 Correct 24 ms 6296 KB Output is correct
12 Correct 19 ms 5856 KB Output is correct
13 Correct 34 ms 7956 KB Output is correct
14 Correct 21 ms 5976 KB Output is correct
15 Correct 23 ms 5200 KB Output is correct
16 Correct 21 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 3 ms 20824 KB Output is correct
7 Correct 3 ms 20828 KB Output is correct
8 Correct 4 ms 21080 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 35 ms 26192 KB Output is correct
11 Correct 39 ms 26448 KB Output is correct
12 Correct 53 ms 27984 KB Output is correct
13 Correct 54 ms 28480 KB Output is correct
14 Correct 65 ms 27728 KB Output is correct
15 Correct 37 ms 25680 KB Output is correct
16 Correct 37 ms 25680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 3 ms 20824 KB Output is correct
7 Correct 3 ms 20828 KB Output is correct
8 Correct 4 ms 21080 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 35 ms 26192 KB Output is correct
11 Correct 39 ms 26448 KB Output is correct
12 Correct 53 ms 27984 KB Output is correct
13 Correct 54 ms 28480 KB Output is correct
14 Correct 65 ms 27728 KB Output is correct
15 Correct 37 ms 25680 KB Output is correct
16 Correct 37 ms 25680 KB Output is correct
17 Correct 422 ms 397036 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4440 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4440 KB Output is correct
23 Correct 19 ms 166736 KB Output is correct
24 Correct 19 ms 166748 KB Output is correct
25 Correct 20 ms 164688 KB Output is correct
26 Correct 19 ms 162648 KB Output is correct
27 Correct 351 ms 395856 KB Output is correct
28 Correct 557 ms 398160 KB Output is correct
29 Correct 488 ms 398700 KB Output is correct
30 Correct 728 ms 397588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 722 ms 354112 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 13 ms 117336 KB Output is correct
6 Correct 13 ms 115288 KB Output is correct
7 Correct 13 ms 113372 KB Output is correct
8 Correct 721 ms 384340 KB Output is correct
9 Correct 688 ms 395740 KB Output is correct
10 Correct 253 ms 393704 KB Output is correct
11 Correct 23 ms 6232 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2392 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 2 ms 2648 KB Output is correct
18 Correct 3 ms 2648 KB Output is correct
19 Correct 15 ms 5620 KB Output is correct
20 Correct 32 ms 7760 KB Output is correct
21 Correct 24 ms 6296 KB Output is correct
22 Correct 19 ms 5856 KB Output is correct
23 Correct 34 ms 7956 KB Output is correct
24 Correct 21 ms 5976 KB Output is correct
25 Correct 23 ms 5200 KB Output is correct
26 Correct 21 ms 5212 KB Output is correct
27 Correct 1 ms 4440 KB Output is correct
28 Correct 1 ms 4440 KB Output is correct
29 Correct 1 ms 4440 KB Output is correct
30 Correct 1 ms 4440 KB Output is correct
31 Correct 1 ms 4440 KB Output is correct
32 Correct 3 ms 20824 KB Output is correct
33 Correct 3 ms 20828 KB Output is correct
34 Correct 4 ms 21080 KB Output is correct
35 Correct 4 ms 21080 KB Output is correct
36 Correct 35 ms 26192 KB Output is correct
37 Correct 39 ms 26448 KB Output is correct
38 Correct 53 ms 27984 KB Output is correct
39 Correct 54 ms 28480 KB Output is correct
40 Correct 65 ms 27728 KB Output is correct
41 Correct 37 ms 25680 KB Output is correct
42 Correct 37 ms 25680 KB Output is correct
43 Correct 422 ms 397036 KB Output is correct
44 Correct 1 ms 4440 KB Output is correct
45 Correct 1 ms 4444 KB Output is correct
46 Correct 1 ms 4440 KB Output is correct
47 Correct 1 ms 4444 KB Output is correct
48 Correct 1 ms 4440 KB Output is correct
49 Correct 19 ms 166736 KB Output is correct
50 Correct 19 ms 166748 KB Output is correct
51 Correct 20 ms 164688 KB Output is correct
52 Correct 19 ms 162648 KB Output is correct
53 Correct 351 ms 395856 KB Output is correct
54 Correct 557 ms 398160 KB Output is correct
55 Correct 488 ms 398700 KB Output is correct
56 Correct 728 ms 397588 KB Output is correct
57 Correct 1213 ms 399232 KB Output is correct
58 Correct 719 ms 396308 KB Output is correct
59 Correct 795 ms 396540 KB Output is correct