Submission #244953

# Submission time Handle Problem Language Result Execution time Memory
244953 2020-07-05T10:01:48 Z rama_pang Olympic Bus (JOI20_ho_t4) C++14
100 / 100
507 ms 3828 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 205;
const int MAXM = 50005;

int N, M;
vector<int> adj[MAXN];
vector<int> radj[MAXN];
vector<array<int, 4>> edge;

int par[5][MAXN];
int dist[5][MAXN];
int sptree[5][MAXM];

void DijkstraSparse(int s, int k, vector<int> a[MAXN]) {
  for (int i = 0; i < MAXN; i++) {
    dist[k][i] = 2e9;
  }
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
  pq.emplace(0, s);
  auto dt = dist[k];
  auto pr = par[k];
  dt[s] = 0;
  pr[s] = -1;
  while (!pq.empty()) {
    int u = pq.top().second;
    int d = pq.top().first;
    pq.pop();
    if (dt[u] != d) {
      continue;
    }
    if (pr[u] != -1) {
      sptree[k][pr[u]] = 1;
    }
    for (auto e : a[u]) {
      int v = edge[e][0] ^ edge[e][1] ^ u;
      if (dt[v] > dt[u] + edge[e][2]) {
        dt[v] = dt[u] + edge[e][2];
        pr[v] = e;
        pq.emplace(dt[v], v);
      }
    }
  }
}

int DijkstraDense(int s, int t, int block, int ru, int rv, int rc) {
  int weight[MAXN][MAXN];
  for (int i = 1; i <= N; i++) {
    dist[4][i] = 2e9;
    for (int j = 1; j <= N; j++) {
      weight[i][j] = 2e9;
    }
  }
  for (int i = 0; i < M; i++) if (i != block) {
    int u, v, c;
    u = edge[i][0], v = edge[i][1], c = edge[i][2];
    weight[u][v] = min(weight[u][v], c);
  }
  weight[ru][rv] = min(weight[ru][rv], rc);

  int vis[MAXN] = {};
  auto dt = dist[4];
  for (int i = 1; i <= N; i++) {
    dt[i] = 2e9;
  }
  dt[s] = 0;
  for (int rep = 1; rep <= N; rep++) {
    int u = -1;
    for (int i = 1; i <= N; i++) if (!vis[i]) {
      if (u == -1 || dt[u] > dt[i]) {
        u = i;
      }
    }
    if (dt[u] == 2e9) {
      break;
    }
    vis[u] = 1;
    for (int v = 1; v <= N; v++) if (weight[u][v] < 2e9) {
      dt[v] = min(dt[v], dt[u] + weight[u][v]);
    }
  }
  return dt[t];
}

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

  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int u, v, c, d;
    cin >> u >> v >> c >> d;
    edge.push_back({u, v, c, d});
    adj[u].emplace_back(i);
    radj[v].emplace_back(i);
  }

  DijkstraSparse(1, 0, adj); // 1 to N
  DijkstraSparse(N, 1, adj); // N to 1
  DijkstraSparse(N, 2, radj); // 1 to N, reverse
  DijkstraSparse(1, 3, radj); // N to 1, reverse

  int ans = (int) min((long long) 2e9, 1ll * dist[0][N] + 1ll * dist[1][1]);
  for (int i = 0; i < M; i++) {
    int u, v, c, d;
    u = edge[i][0], v = edge[i][1], c = edge[i][2], d = edge[i][3];
    if (sptree[0][i] == 0 && sptree[1][i] == 0) {
      int d1 = min((long long) dist[0][N], (long long) dist[0][v] + c + dist[2][u]);
      int d2 = min((long long) dist[1][1], (long long) dist[1][v] + c + dist[3][u]);
      if (d1 < 2e9 && d2 < 2e9) {
        ans = min(ans, d + d1 + d2);
      }
    } else {
      int d1 = DijkstraDense(1, N, i, v, u, c);
      int d2 = DijkstraDense(N, 1, i, v, u, c);
      if (d1 < 2e9 && d2 < 2e9) {
        ans = min(ans, d + d1 + d2);
      }
    }
  }
  if (ans >= 2e9) {
    ans = -1;
  }
  cout << ans << "\n";
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 90 ms 640 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 118 ms 640 KB Output is correct
4 Correct 134 ms 640 KB Output is correct
5 Correct 7 ms 576 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 94 ms 564 KB Output is correct
11 Correct 117 ms 760 KB Output is correct
12 Correct 116 ms 640 KB Output is correct
13 Correct 46 ms 544 KB Output is correct
14 Correct 92 ms 672 KB Output is correct
15 Correct 76 ms 636 KB Output is correct
16 Correct 95 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 471 ms 2804 KB Output is correct
2 Correct 499 ms 2668 KB Output is correct
3 Correct 468 ms 2680 KB Output is correct
4 Correct 126 ms 640 KB Output is correct
5 Correct 88 ms 760 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 8 ms 512 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 189 ms 2664 KB Output is correct
10 Correct 182 ms 2664 KB Output is correct
11 Correct 337 ms 2676 KB Output is correct
12 Correct 332 ms 2672 KB Output is correct
13 Correct 309 ms 2676 KB Output is correct
14 Correct 278 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 640 KB Output is correct
2 Correct 17 ms 512 KB Output is correct
3 Correct 483 ms 2164 KB Output is correct
4 Correct 8 ms 512 KB Output is correct
5 Correct 507 ms 2676 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 220 ms 2524 KB Output is correct
9 Correct 247 ms 2524 KB Output is correct
10 Correct 340 ms 2540 KB Output is correct
11 Correct 332 ms 2548 KB Output is correct
12 Correct 361 ms 2580 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 354 ms 2772 KB Output is correct
20 Correct 337 ms 2668 KB Output is correct
21 Correct 340 ms 2676 KB Output is correct
22 Correct 335 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 640 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 118 ms 640 KB Output is correct
4 Correct 134 ms 640 KB Output is correct
5 Correct 7 ms 576 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 94 ms 564 KB Output is correct
11 Correct 117 ms 760 KB Output is correct
12 Correct 116 ms 640 KB Output is correct
13 Correct 46 ms 544 KB Output is correct
14 Correct 92 ms 672 KB Output is correct
15 Correct 76 ms 636 KB Output is correct
16 Correct 95 ms 640 KB Output is correct
17 Correct 471 ms 2804 KB Output is correct
18 Correct 499 ms 2668 KB Output is correct
19 Correct 468 ms 2680 KB Output is correct
20 Correct 126 ms 640 KB Output is correct
21 Correct 88 ms 760 KB Output is correct
22 Correct 5 ms 512 KB Output is correct
23 Correct 8 ms 512 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 189 ms 2664 KB Output is correct
26 Correct 182 ms 2664 KB Output is correct
27 Correct 337 ms 2676 KB Output is correct
28 Correct 332 ms 2672 KB Output is correct
29 Correct 309 ms 2676 KB Output is correct
30 Correct 278 ms 2772 KB Output is correct
31 Correct 103 ms 640 KB Output is correct
32 Correct 17 ms 512 KB Output is correct
33 Correct 483 ms 2164 KB Output is correct
34 Correct 8 ms 512 KB Output is correct
35 Correct 507 ms 2676 KB Output is correct
36 Correct 5 ms 384 KB Output is correct
37 Correct 4 ms 384 KB Output is correct
38 Correct 220 ms 2524 KB Output is correct
39 Correct 247 ms 2524 KB Output is correct
40 Correct 340 ms 2540 KB Output is correct
41 Correct 332 ms 2548 KB Output is correct
42 Correct 361 ms 2580 KB Output is correct
43 Correct 5 ms 384 KB Output is correct
44 Correct 5 ms 384 KB Output is correct
45 Correct 5 ms 384 KB Output is correct
46 Correct 5 ms 384 KB Output is correct
47 Correct 4 ms 384 KB Output is correct
48 Correct 5 ms 384 KB Output is correct
49 Correct 354 ms 2772 KB Output is correct
50 Correct 337 ms 2668 KB Output is correct
51 Correct 340 ms 2676 KB Output is correct
52 Correct 335 ms 2668 KB Output is correct
53 Correct 403 ms 2808 KB Output is correct
54 Correct 443 ms 3052 KB Output is correct
55 Correct 425 ms 2796 KB Output is correct
56 Correct 142 ms 640 KB Output is correct
57 Correct 109 ms 640 KB Output is correct
58 Correct 297 ms 2420 KB Output is correct
59 Correct 347 ms 2324 KB Output is correct
60 Correct 455 ms 2412 KB Output is correct
61 Correct 295 ms 2420 KB Output is correct
62 Correct 337 ms 2412 KB Output is correct
63 Correct 453 ms 2412 KB Output is correct
64 Correct 229 ms 2788 KB Output is correct
65 Correct 268 ms 2668 KB Output is correct
66 Correct 331 ms 2660 KB Output is correct
67 Correct 24 ms 2632 KB Output is correct
68 Correct 183 ms 3808 KB Output is correct
69 Correct 178 ms 3800 KB Output is correct
70 Correct 315 ms 3820 KB Output is correct
71 Correct 317 ms 3828 KB Output is correct
72 Correct 321 ms 3828 KB Output is correct
73 Correct 332 ms 3828 KB Output is correct
74 Correct 317 ms 3820 KB Output is correct