Submission #1026484

# Submission time Handle Problem Language Result Execution time Memory
1026484 2024-07-18T06:47:56 Z NeroZein Olympic Bus (JOI20_ho_t4) C++17
100 / 100
860 ms 2904 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif

using pii = pair<int, int>;

const int N = 202;
const int INF = 2e9 + 9;

struct edge {
  int from, to, c, d;
};

int n, m;
int pe[N], pv[N];
vector<int> g[N];

vector<int> dijkstra(int src, vector<edge>& e) {
  vector<int> dist(n + 1, INF);
  dist[src] = 0; 
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push({0, src});
  while (!pq.empty()) {
    auto [c, v] = pq.top();
    pq.pop();
    if (c != dist[v]) {
      continue; 
    }
    for (int i : g[v]) {
      int u = v ^ e[i].from ^ e[i].to;
      if (c + e[i].c < dist[u]) {
        pe[u] = i;
        pv[u] = v;
        dist[u] = c + e[i].c;
        pq.push({dist[u], u});
      }
    }
  }
  return dist;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m;
  vector<edge> e(m);
  for (int i = 0; i < m; ++i) {
    int u, v, c, d;
    cin >> u >> v >> c >> d;
    e[i] = {u, v, c, d}; 
    g[u].push_back(i); 
  }
  vector<bool> marked(m);
  vector<vector<int>> dist(n + 1); 
  dist[1] = dijkstra(1, e);
  if (dist[1][n] != INF) {
    int x = n;
    while (x != 1) {
      assert(pv[x] != 0);
      marked[pe[x]] = true;
      x = pv[x]; 
    }
  }
  for (int i = 1; i <= n; ++i) {
    pe[i] = pv[i] = 0; 
  }
  dist[n] = dijkstra(n, e);
  if (dist[n][1] != INF) {
    int x = 1;
    while (x != n) {
      assert(pv[x] != 0);
      marked[pe[x]] = true;
      x = pv[x]; 
    }    
  }
  int ans = INF;
  if (dist[1][n] != INF && dist[n][1] != INF) {
    ans = dist[1][n] + dist[n][1];
  }
  for (int i = 2; i < n; ++i) {
    dist[i] = dijkstra(i, e);
  }
  for (int i = 0; i < m; ++i) {
    if (marked[i]) {
      continue; 
    }
    int u = e[i].from, v = e[i].to; 
    if (dist[1][n] != INF && dist[n][v] != INF && dist[u][1] != INF) {
      ans = min(ans, e[i].d + dist[1][n] + dist[n][v] + e[i].c + dist[u][1]);
    }
    if (dist[1][v] != INF && dist[u][n] != INF && dist[n][1] != INF) {
      ans = min(ans, e[i].d + dist[1][v] + e[i].c + dist[u][n] + dist[n][1]);
    }
    if (dist[1][v] != INF && dist[u][n] != INF && dist[n][v] != INF && dist[u][1] != INF) {
      ans = min(ans, e[i].d + dist[1][v] + e[i].c + dist[u][n] + dist[n][v] + e[i].c + dist[u][1]);
    }
  }
  for (int i = 0; i < m; ++i) {
    if (!marked[i]) {
      continue; 
    }
    int u = e[i].from, v = e[i].to;
    g[u].erase(find(g[u].begin(), g[u].end(), i));
    g[v].push_back(i);
    swap(e[i].from, e[i].to);//useless, but for the logic
    dist[1] = dijkstra(1, e); 
    dist[n] = dijkstra(n, e);
    if (dist[1][n] != INF && dist[n][1] != INF) {
      ans = min(ans, e[i].d + dist[1][n] + dist[n][1]); 
    }
    g[v].pop_back();
    g[u].push_back(i);
    swap(e[i].from, e[i].to); 
  }
  if (ans == INF) {
    ans = -1; 
  }
  cout << ans << '\n'; 
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 7 ms 600 KB Output is correct
4 Correct 6 ms 608 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 19 ms 664 KB Output is correct
11 Correct 20 ms 604 KB Output is correct
12 Correct 21 ms 504 KB Output is correct
13 Correct 4 ms 540 KB Output is correct
14 Correct 4 ms 604 KB Output is correct
15 Correct 4 ms 604 KB Output is correct
16 Correct 4 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 1628 KB Output is correct
2 Correct 54 ms 2644 KB Output is correct
3 Correct 55 ms 2900 KB Output is correct
4 Correct 7 ms 604 KB Output is correct
5 Correct 4 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 0 ms 360 KB Output is correct
9 Correct 38 ms 2776 KB Output is correct
10 Correct 42 ms 2900 KB Output is correct
11 Correct 48 ms 2816 KB Output is correct
12 Correct 44 ms 2904 KB Output is correct
13 Correct 49 ms 2808 KB Output is correct
14 Correct 43 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 37 ms 1468 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 40 ms 1628 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 30 ms 1656 KB Output is correct
9 Correct 29 ms 1676 KB Output is correct
10 Correct 38 ms 1648 KB Output is correct
11 Correct 31 ms 1608 KB Output is correct
12 Correct 40 ms 1628 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 464 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 33 ms 2648 KB Output is correct
20 Correct 31 ms 2652 KB Output is correct
21 Correct 32 ms 2644 KB Output is correct
22 Correct 32 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 7 ms 600 KB Output is correct
4 Correct 6 ms 608 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 19 ms 664 KB Output is correct
11 Correct 20 ms 604 KB Output is correct
12 Correct 21 ms 504 KB Output is correct
13 Correct 4 ms 540 KB Output is correct
14 Correct 4 ms 604 KB Output is correct
15 Correct 4 ms 604 KB Output is correct
16 Correct 4 ms 604 KB Output is correct
17 Correct 54 ms 1628 KB Output is correct
18 Correct 54 ms 2644 KB Output is correct
19 Correct 55 ms 2900 KB Output is correct
20 Correct 7 ms 604 KB Output is correct
21 Correct 4 ms 604 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 0 ms 360 KB Output is correct
25 Correct 38 ms 2776 KB Output is correct
26 Correct 42 ms 2900 KB Output is correct
27 Correct 48 ms 2816 KB Output is correct
28 Correct 44 ms 2904 KB Output is correct
29 Correct 49 ms 2808 KB Output is correct
30 Correct 43 ms 2900 KB Output is correct
31 Correct 3 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 37 ms 1468 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 40 ms 1628 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 30 ms 1656 KB Output is correct
39 Correct 29 ms 1676 KB Output is correct
40 Correct 38 ms 1648 KB Output is correct
41 Correct 31 ms 1608 KB Output is correct
42 Correct 40 ms 1628 KB Output is correct
43 Correct 0 ms 344 KB Output is correct
44 Correct 0 ms 464 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 33 ms 2648 KB Output is correct
50 Correct 31 ms 2652 KB Output is correct
51 Correct 32 ms 2644 KB Output is correct
52 Correct 32 ms 2648 KB Output is correct
53 Correct 59 ms 2652 KB Output is correct
54 Correct 59 ms 2652 KB Output is correct
55 Correct 58 ms 2644 KB Output is correct
56 Correct 6 ms 604 KB Output is correct
57 Correct 5 ms 604 KB Output is correct
58 Correct 119 ms 2264 KB Output is correct
59 Correct 135 ms 2140 KB Output is correct
60 Correct 172 ms 2140 KB Output is correct
61 Correct 124 ms 2140 KB Output is correct
62 Correct 129 ms 2300 KB Output is correct
63 Correct 165 ms 2140 KB Output is correct
64 Correct 694 ms 2540 KB Output is correct
65 Correct 710 ms 2548 KB Output is correct
66 Correct 860 ms 2568 KB Output is correct
67 Correct 8 ms 2140 KB Output is correct
68 Correct 43 ms 2900 KB Output is correct
69 Correct 40 ms 2804 KB Output is correct
70 Correct 45 ms 2900 KB Output is correct
71 Correct 47 ms 2652 KB Output is correct
72 Correct 47 ms 2652 KB Output is correct
73 Correct 46 ms 2900 KB Output is correct
74 Correct 45 ms 2652 KB Output is correct