Submission #532355

# Submission time Handle Problem Language Result Execution time Memory
532355 2022-03-02T18:18:58 Z Alex_tz307 Autobus (COCI22_autobus) C++17
70 / 70
167 ms 6324 KB
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

const int64_t INFL = 1e18L;

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

void testCase() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> wt(n + 1, vector<int>(n + 1, INF));
  for (int i = 0; i < m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    minSelf(wt[u][v], w);
  }
  vector<vector<pair<int, int>>> g(n + 1);
  for (int u = 1; u <= n; ++u) {
    for (int v = 1; v <= n; ++v) {
      if (u != v && wt[u][v] != INF) {
        g[u].emplace_back(v, wt[u][v]);
      }
    }
  }
  int k, q;
  cin >> k >> q;
  minSelf(k, n);
  vector<vector<int64_t>> best(n + 1, vector<int64_t>(n + 1, INFL));
  for (int s = 1; s <= n; ++s) {
    vector<vector<int64_t>> dp(n + 1, vector<int64_t>(k + 1, INFL)); /// costul minim sa ajung de la s la nod cu lungime len
    vector<vector<bool>> inQ(n + 1, vector<bool>(k + 1));
    dp[s][0] = 0;
    queue<pair<int, int>> q;
    q.emplace(s, 0);
    inQ[s][0] = true;
    while (!q.empty()) {
      int u, len;
      tie(u, len) = q.front();
      q.pop();
      inQ[u][len] = false;
      for (auto it : g[u]) {
        int v, w;
        tie(v, w) = it;
        if (dp[v][len + 1] > dp[u][len] + w) {
          dp[v][len + 1] = dp[u][len] + w;
          if (len + 1 < k && !inQ[v][len + 1]) {
            q.emplace(v, len + 1);
            inQ[v][len + 1] = true;
          }
        }
      }
    }
    for (int t = 1; t <= n; ++t) {
      best[s][t] = *min_element(dp[t].begin(), dp[t].end());
    }
  }
  for (int i = 0; i < q; ++i) {
    int s, t;
    cin >> s >> t;
    if (best[s][t] == INFL) {
      cout << "-1\n";
    } else {
      cout << best[s][t] << '\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 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 3 ms 332 KB Output is correct
6 Correct 4 ms 332 KB Output is correct
7 Correct 4 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 8 ms 460 KB Output is correct
8 Correct 17 ms 444 KB Output is correct
9 Correct 3 ms 332 KB Output is correct
10 Correct 15 ms 304 KB Output is correct
11 Correct 9 ms 476 KB Output is correct
12 Correct 33 ms 448 KB Output is correct
13 Correct 94 ms 1220 KB Output is correct
14 Correct 95 ms 1264 KB Output is correct
15 Correct 133 ms 1716 KB Output is correct
16 Correct 114 ms 5972 KB Output is correct
17 Correct 97 ms 2096 KB Output is correct
18 Correct 98 ms 4280 KB Output is correct
19 Correct 137 ms 3476 KB Output is correct
20 Correct 125 ms 5444 KB Output is correct
21 Correct 96 ms 4292 KB Output is correct
22 Correct 103 ms 5680 KB Output is correct
23 Correct 104 ms 444 KB Output is correct
24 Correct 126 ms 3080 KB Output is correct
25 Correct 93 ms 4188 KB Output is correct
26 Correct 101 ms 472 KB Output is correct
27 Correct 106 ms 456 KB Output is correct
28 Correct 101 ms 464 KB Output is correct
29 Correct 93 ms 464 KB Output is correct
30 Correct 103 ms 4752 KB Output is correct
31 Correct 133 ms 6324 KB Output is correct
32 Correct 149 ms 5992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 3 ms 332 KB Output is correct
12 Correct 4 ms 332 KB Output is correct
13 Correct 4 ms 460 KB Output is correct
14 Correct 8 ms 460 KB Output is correct
15 Correct 17 ms 444 KB Output is correct
16 Correct 3 ms 332 KB Output is correct
17 Correct 15 ms 304 KB Output is correct
18 Correct 9 ms 476 KB Output is correct
19 Correct 33 ms 448 KB Output is correct
20 Correct 94 ms 1220 KB Output is correct
21 Correct 95 ms 1264 KB Output is correct
22 Correct 133 ms 1716 KB Output is correct
23 Correct 114 ms 5972 KB Output is correct
24 Correct 97 ms 2096 KB Output is correct
25 Correct 98 ms 4280 KB Output is correct
26 Correct 137 ms 3476 KB Output is correct
27 Correct 125 ms 5444 KB Output is correct
28 Correct 96 ms 4292 KB Output is correct
29 Correct 103 ms 5680 KB Output is correct
30 Correct 104 ms 444 KB Output is correct
31 Correct 126 ms 3080 KB Output is correct
32 Correct 93 ms 4188 KB Output is correct
33 Correct 101 ms 472 KB Output is correct
34 Correct 106 ms 456 KB Output is correct
35 Correct 101 ms 464 KB Output is correct
36 Correct 93 ms 464 KB Output is correct
37 Correct 103 ms 4752 KB Output is correct
38 Correct 133 ms 6324 KB Output is correct
39 Correct 149 ms 5992 KB Output is correct
40 Correct 6 ms 440 KB Output is correct
41 Correct 18 ms 444 KB Output is correct
42 Correct 5 ms 316 KB Output is correct
43 Correct 25 ms 460 KB Output is correct
44 Correct 9 ms 488 KB Output is correct
45 Correct 47 ms 468 KB Output is correct
46 Correct 91 ms 1260 KB Output is correct
47 Correct 122 ms 3896 KB Output is correct
48 Correct 111 ms 2244 KB Output is correct
49 Correct 145 ms 5744 KB Output is correct
50 Correct 95 ms 4540 KB Output is correct
51 Correct 105 ms 4736 KB Output is correct
52 Correct 90 ms 5700 KB Output is correct
53 Correct 143 ms 4860 KB Output is correct
54 Correct 104 ms 1312 KB Output is correct
55 Correct 91 ms 3552 KB Output is correct
56 Correct 149 ms 6260 KB Output is correct
57 Correct 145 ms 3140 KB Output is correct
58 Correct 86 ms 452 KB Output is correct
59 Correct 112 ms 484 KB Output is correct
60 Correct 132 ms 452 KB Output is correct
61 Correct 153 ms 724 KB Output is correct
62 Correct 85 ms 540 KB Output is correct
63 Correct 99 ms 428 KB Output is correct
64 Correct 105 ms 568 KB Output is correct
65 Correct 167 ms 4092 KB Output is correct