Submission #827631

# Submission time Handle Problem Language Result Execution time Memory
827631 2023-08-16T15:28:42 Z taher Olympic Bus (JOI20_ho_t4) C++17
100 / 100
115 ms 6928 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#ifdef LOCAL
#include "C:\GCC\debug.h"
#else
#define debug(...) void(42)
#endif
 
const long long inf = 1165465792545;
const int N = 305;

vector<array<int, 4>> adj[N], inv[N];
long long d2[N][N], d1[N][N], dist[N];
long long between[N][N];
int count_edges[N][N];
int save_edge[N][N];

int cntA[N], cntB[N], cntC[N], cntD[N];
int middleA[N], middleB[N], middleC[N], middleD[N];
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
 
  int n, m;
  cin >> n >> m;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      between[i][j] = inf;
    }
  }
  vector<array<int, 4>> edges;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    int c, d;
    cin >> c >> d;
    --u, --v;
    adj[u].push_back({v, c, d, i});
    inv[v].push_back({u, c, d, i});
    edges.push_back({u, v, c, d});

    if (between[u][v] > c) {
      between[u][v] = c;
      count_edges[u][v] = 1;
      save_edge[u][v] = i;
    } else if (between[u][v] == c) {
      count_edges[u][v] += 1;
    }
  }
 
  array<int, 2> perm;
  perm[0] = 0;
  perm[1] = n - 1;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      d2[i][j] = inf;
      d1[i][j] = inf;
    }
  }
 
  auto Floyd = [&]() {
    for (int i = 0; i < n; i++) {
      d2[i][i] = 0;
      d1[i][i] = 0;
    }
    for (int i = 0; i < n; i++) {
      for (auto v : adj[i]) {
        d2[i][v[0]] = min(d2[i][v[0]], 1LL * v[1]);
        d1[i][v[0]] = min(d1[i][v[0]], 1LL * (v[1] + 1));
      }
    }
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          d2[i][j] = min(d2[i][j], d2[i][k] + d2[k][j]);
          d1[i][j] = min(d1[i][j], d1[i][k] + d1[k][j]);
        }
      }
    }
  };
 
  Floyd();
  auto Update = [&](int forbidden, int source, int to, int cost) {
    for (int i = 0; i < n; i++) {
      dist[i] = inf;
    }
    vector<bool> u(n);
    dist[perm[1]] = 0;

    for (int i = 0; i < n; i++) {
      int v = -1;
      for (int j = 0; j < n; j++) {
        if (!u[j] && (v == -1 || dist[j] < dist[v])) {
          v = j;
        }
      }

      if (v == -1 || dist[v] == inf) {
        break;
      }
      u[v] = true;

      if (v == source) {
        if (dist[to] > 1LL * dist[v] + cost) {
          dist[to] = dist[v] + cost;
        }
      }

      for (auto edge : adj[v]) {
        if (edge[3] == forbidden) {
          continue;
        }
        if (dist[v] + edge[1] < dist[edge[0]]) {
          dist[edge[0]] = dist[v] + edge[1];
        }
      }
    }
    return dist[perm[0]];
  };

  for (int i = 0; i < n; i++) {
    middleA[i] = middleB[i] = middleC[i] = middleD[i] = -1;
  }
 
  for (int i = 0; i < n; i++) {
    for (auto v : inv[i]) {
      if (d1[perm[0]][v[0]] + (v[1] + 1) == d1[perm[0]][i]) {
        cntA[i]++;
        middleA[i] = v[3];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (auto v : adj[i]) {
      if (d1[v[0]][perm[1]] + (v[1] + 1) == d1[i][perm[1]]) {
        cntB[i]++;
        middleB[i] = v[3];
      }
    }
  }
 
  for (int i = 0; i < n; i++) {
    for (auto v : inv[i]) {
      if (d1[perm[1]][v[0]] + (v[1] + 1) == d1[perm[1]][i]) {
        cntC[i]++;
        middleC[i] = v[3];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (auto v : adj[i]) {
      if (d1[v[0]][perm[0]] + (v[1] + 1) == d1[i][perm[0]]) {
        cntD[i]++;
        middleD[i] = v[3];
      }
    }
  }

  long long res = inf;
  res = min(res, d2[perm[0]][perm[1]] + d2[perm[1]][perm[0]]);

  int counter = 0;
  for (int i = 0; i < m; i++) {
    int u = edges[i][0];
    int v = edges[i][1];
    int c = edges[i][2];
    int d = edges[i][3];
 
    long long path_from_beg = 0;
 
    bool canA = true;
    bool canB = true;
 
    canA &= (cntA[v] > 1 || middleA[v] != i);
    canB &= (cntB[u] > 1 || middleB[u] != i);

    if (canA && canB) {
      path_from_beg = min(d2[perm[0]][perm[1]], d2[perm[0]][v] + d2[u][perm[1]] + c);
    } else {
      swap(perm[0], perm[1]);
      path_from_beg = Update(i, v, u, c);
      swap(perm[0], perm[1]);
    }
 
    canA = true;
    canB = true;
 
    canA &= (cntC[v] > 1 || middleC[v] != i);
    canB &= (cntD[u] > 1 || middleD[u] != i);

    long long path_from_end = 0;
    if (canA && canB) {
      path_from_end = min(d2[perm[1]][perm[0]], d2[perm[1]][v] + d2[u][perm[0]] + c);
    } else {
      path_from_end = Update(i, v, u, c);
    }
 
    long long tot = path_from_beg + path_from_end + d;
    res = min(res, tot);
  }
 
  if (res == inf) {
    cout << -1 << '\n';
  } else {
    cout << res << '\n';
  }
  return 0;
}

Compilation message

ho_t4.cpp: In function 'int main()':
ho_t4.cpp:167:7: warning: unused variable 'counter' [-Wunused-variable]
  167 |   int counter = 0;
      |       ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 45 ms 2260 KB Output is correct
2 Correct 18 ms 2260 KB Output is correct
3 Correct 64 ms 2360 KB Output is correct
4 Correct 73 ms 2480 KB Output is correct
5 Correct 1 ms 740 KB Output is correct
6 Correct 17 ms 2260 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 424 KB Output is correct
10 Correct 46 ms 2368 KB Output is correct
11 Correct 45 ms 2260 KB Output is correct
12 Correct 51 ms 2260 KB Output is correct
13 Correct 31 ms 2376 KB Output is correct
14 Correct 50 ms 2376 KB Output is correct
15 Correct 46 ms 2272 KB Output is correct
16 Correct 48 ms 2352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 6324 KB Output is correct
2 Correct 32 ms 6328 KB Output is correct
3 Correct 32 ms 6336 KB Output is correct
4 Correct 13 ms 2456 KB Output is correct
5 Correct 14 ms 2356 KB Output is correct
6 Correct 13 ms 2312 KB Output is correct
7 Correct 19 ms 2292 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 34 ms 6300 KB Output is correct
10 Correct 29 ms 6372 KB Output is correct
11 Correct 35 ms 6500 KB Output is correct
12 Correct 44 ms 6368 KB Output is correct
13 Correct 47 ms 6344 KB Output is correct
14 Correct 29 ms 6752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 2376 KB Output is correct
2 Correct 19 ms 2268 KB Output is correct
3 Correct 57 ms 5396 KB Output is correct
4 Correct 15 ms 2260 KB Output is correct
5 Correct 62 ms 6544 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 48 ms 6548 KB Output is correct
9 Correct 38 ms 6504 KB Output is correct
10 Correct 41 ms 6472 KB Output is correct
11 Correct 49 ms 6520 KB Output is correct
12 Correct 41 ms 6580 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 38 ms 6632 KB Output is correct
20 Correct 41 ms 6576 KB Output is correct
21 Correct 39 ms 6472 KB Output is correct
22 Correct 36 ms 6424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 2260 KB Output is correct
2 Correct 18 ms 2260 KB Output is correct
3 Correct 64 ms 2360 KB Output is correct
4 Correct 73 ms 2480 KB Output is correct
5 Correct 1 ms 740 KB Output is correct
6 Correct 17 ms 2260 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 424 KB Output is correct
10 Correct 46 ms 2368 KB Output is correct
11 Correct 45 ms 2260 KB Output is correct
12 Correct 51 ms 2260 KB Output is correct
13 Correct 31 ms 2376 KB Output is correct
14 Correct 50 ms 2376 KB Output is correct
15 Correct 46 ms 2272 KB Output is correct
16 Correct 48 ms 2352 KB Output is correct
17 Correct 29 ms 6324 KB Output is correct
18 Correct 32 ms 6328 KB Output is correct
19 Correct 32 ms 6336 KB Output is correct
20 Correct 13 ms 2456 KB Output is correct
21 Correct 14 ms 2356 KB Output is correct
22 Correct 13 ms 2312 KB Output is correct
23 Correct 19 ms 2292 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 34 ms 6300 KB Output is correct
26 Correct 29 ms 6372 KB Output is correct
27 Correct 35 ms 6500 KB Output is correct
28 Correct 44 ms 6368 KB Output is correct
29 Correct 47 ms 6344 KB Output is correct
30 Correct 29 ms 6752 KB Output is correct
31 Correct 53 ms 2376 KB Output is correct
32 Correct 19 ms 2268 KB Output is correct
33 Correct 57 ms 5396 KB Output is correct
34 Correct 15 ms 2260 KB Output is correct
35 Correct 62 ms 6544 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 48 ms 6548 KB Output is correct
39 Correct 38 ms 6504 KB Output is correct
40 Correct 41 ms 6472 KB Output is correct
41 Correct 49 ms 6520 KB Output is correct
42 Correct 41 ms 6580 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 38 ms 6632 KB Output is correct
50 Correct 41 ms 6576 KB Output is correct
51 Correct 39 ms 6472 KB Output is correct
52 Correct 36 ms 6424 KB Output is correct
53 Correct 115 ms 6420 KB Output is correct
54 Correct 114 ms 6444 KB Output is correct
55 Correct 111 ms 6512 KB Output is correct
56 Correct 65 ms 2376 KB Output is correct
57 Correct 65 ms 2260 KB Output is correct
58 Correct 86 ms 5448 KB Output is correct
59 Correct 77 ms 5528 KB Output is correct
60 Correct 70 ms 5576 KB Output is correct
61 Correct 72 ms 5492 KB Output is correct
62 Correct 71 ms 5436 KB Output is correct
63 Correct 70 ms 5448 KB Output is correct
64 Correct 113 ms 5720 KB Output is correct
65 Correct 112 ms 5848 KB Output is correct
66 Correct 103 ms 5832 KB Output is correct
67 Correct 17 ms 3396 KB Output is correct
68 Correct 53 ms 6740 KB Output is correct
69 Correct 52 ms 6764 KB Output is correct
70 Correct 83 ms 6800 KB Output is correct
71 Correct 84 ms 6740 KB Output is correct
72 Correct 86 ms 6736 KB Output is correct
73 Correct 90 ms 6928 KB Output is correct
74 Correct 93 ms 6864 KB Output is correct