Submission #442831

# Submission time Handle Problem Language Result Execution time Memory
442831 2021-07-09T08:22:59 Z arujbansal Olympic Bus (JOI20_ho_t4) C++17
100 / 100
276 ms 3616 KB
#include <iostream>
#include <algorithm>
#include <limits>
#include <vector>
#include <map>
#include <set>
#include <array>
#include <stack>
#include <queue>
#include <random>
#include <numeric>
#include <functional>
#include <chrono>
#include <utility>
#include <iomanip>
#include <assert.h>

using namespace std;

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define rng_seed(x) mt19937 rng(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define int long long

template<typename T>
struct Dijkstra {
    const T INF = 1e18;

    int n;
    vector<vector<T>> adj;
    vector<T> dist;
    vector<int> par, path;

    Dijkstra(int _n = 0) { init(_n); }

    void init(int _n) {
        n = _n;
        adj.assign(n, vector<T>(n + 1, INF));
    }

    void minimise_directional_edge(int u, int v, T wt) {
        adj[u][v] = min(adj[u][v], wt);
    }

    void minimise_bidirectional_edge(int u, int v, T wt) {
        minimise_directional_edge(u, v, wt);
        minimise_directional_edge(v, u, wt);
    }

    void set_directional_edge(int u, int v, T wt) {
        adj[u][v] = wt;
    }

    void set_bidirectional_edge(int u, int v, T wt) {
        set_directional_edge(u, v, wt);
        set_directional_edge(v, u, wt);
    }

    T get_edge(int u, int v) {
        return adj[u][v];
    }

    void run(vector<int> src) {
        vector<bool> vis(n, false);
        dist.assign(n, INF);
        par.assign(n, -1);
        path.clear();

        for (const auto &node : src)
            dist[node] = 0;

        while (true) {
            pair<T, int> mn = make_pair(INF, -1);

            for (int u = 0; u < n; u++) {
                if (!vis[u])
                    mn = min(mn, make_pair(dist[u], u));
            }

            if (mn.first >= INF) break;
            vis[mn.second] = true;

            for (int v = 0; v < n; v++) {
                if (adj[mn.second][v] >= INF) continue;

                T new_dist = mn.first + adj[mn.second][v];

                if (new_dist < dist[v]) {
                    dist[v] = new_dist;
                    par[v] = mn.second;
                }
            }
        }
    }

    void construct_path(int dest) {
        path.clear();

        path.push_back(dest);
        while (par[path.back()] > -1)
            path.push_back(par[path.back()]);

        reverse(path.begin(), path.end());
    }

    bool reachable(int node) {
        return par[node] > -1;
    }
};

const int MXN = 205, INF = 1e18;
vector<pair<int, int>> edges[MXN][MXN];
bool in_path[MXN][MXN];

void solve() {
    int N, M;
    cin >> N >> M;

    Dijkstra<int> inp_g(N + 1), inp_g_rev(N + 1);

    while (M--) {
        int u, v, c, d;
        cin >> u >> v >> c >> d;

        inp_g.minimise_directional_edge(u, v, c);
        inp_g_rev.minimise_directional_edge(v, u, c);
        edges[u][v].emplace_back(c, d);
    }

    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= N; j++)
            sort(all(edges[i][j]));

    inp_g.run(vector<int>{N});
    vector<int> dist_t = inp_g.dist;
    inp_g.construct_path(1);

    for (int i = 1; i < sz(inp_g.path); i++)
        in_path[inp_g.path[i - 1]][inp_g.path[i]] = true;

    inp_g.run(vector<int>{1});
    vector<int> dist_s = inp_g.dist;
    inp_g.construct_path(N);

    for (int i = 1; i < sz(inp_g.path); i++)
        in_path[inp_g.path[i - 1]][inp_g.path[i]] = true;

    inp_g_rev.run(vector<int>{1});
    vector<int> dist_s_rev = inp_g_rev.dist;

    inp_g_rev.run(vector<int>{N});
    vector<int> dist_t_rev = inp_g_rev.dist;

    int ans = dist_s[N] + dist_t[1];

    for (int u = 1; u <= N; u++) {
        for (int v = 1; v <= N; v++) {
            if (edges[u][v].empty()) continue;

            if (in_path[u][v]) {
                int inp_g_edge_uv = inp_g.get_edge(u, v);
                int inp_g_edge_vu = inp_g.get_edge(v, u);
                inp_g.set_directional_edge(u, v, sz(edges[u][v]) > 1 ? edges[u][v][1].first : INF);
                inp_g.set_directional_edge(v, u, edges[u][v][0].first);

                inp_g.run(vector<int>{1});
                int cost = inp_g.dist[N];

                inp_g.run(vector<int>{N});
                cost += inp_g.dist[1];

                ans = min(ans, cost + edges[u][v][0].second);
    
                inp_g.set_directional_edge(u, v, inp_g_edge_uv);
                inp_g.set_directional_edge(v, u, inp_g_edge_vu);
            }

            for (int k = 0 + (in_path[u][v]); k < sz(edges[u][v]); k++) {
                int cost1 = edges[u][v][k].first + edges[u][v][k].second + dist_s_rev[u] + dist_t[v]; // T to S
                int cost2 = edges[u][v][k].first + edges[u][v][k].second + dist_s[v] + dist_t_rev[u]; // S to T
                ans = min({ans, cost1 + dist_s[N], cost2 + dist_t[1], cost1 + cost2 - edges[u][v][k].second});
            }
        }
    }

    cout << (ans >= INF ? -1 : ans);
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int TC = 1;
    // cin >> TC;
    while (TC--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1996 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 6 ms 1996 KB Output is correct
4 Correct 8 ms 1996 KB Output is correct
5 Correct 2 ms 1356 KB Output is correct
6 Correct 2 ms 1868 KB Output is correct
7 Correct 1 ms 1228 KB Output is correct
8 Correct 1 ms 1228 KB Output is correct
9 Correct 1 ms 1356 KB Output is correct
10 Correct 81 ms 2016 KB Output is correct
11 Correct 111 ms 2044 KB Output is correct
12 Correct 129 ms 2028 KB Output is correct
13 Correct 3 ms 1996 KB Output is correct
14 Correct 8 ms 1996 KB Output is correct
15 Correct 5 ms 1996 KB Output is correct
16 Correct 8 ms 1996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 3040 KB Output is correct
2 Correct 37 ms 3112 KB Output is correct
3 Correct 40 ms 3060 KB Output is correct
4 Correct 6 ms 2020 KB Output is correct
5 Correct 10 ms 1996 KB Output is correct
6 Correct 3 ms 1996 KB Output is correct
7 Correct 2 ms 1868 KB Output is correct
8 Correct 1 ms 1228 KB Output is correct
9 Correct 31 ms 3148 KB Output is correct
10 Correct 29 ms 3244 KB Output is correct
11 Correct 37 ms 3268 KB Output is correct
12 Correct 36 ms 3264 KB Output is correct
13 Correct 38 ms 3204 KB Output is correct
14 Correct 34 ms 3184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1996 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 27 ms 2952 KB Output is correct
4 Correct 2 ms 1996 KB Output is correct
5 Correct 30 ms 3340 KB Output is correct
6 Correct 1 ms 1228 KB Output is correct
7 Correct 1 ms 1228 KB Output is correct
8 Correct 30 ms 3360 KB Output is correct
9 Correct 29 ms 3396 KB Output is correct
10 Correct 30 ms 3484 KB Output is correct
11 Correct 33 ms 3404 KB Output is correct
12 Correct 31 ms 3392 KB Output is correct
13 Correct 1 ms 1228 KB Output is correct
14 Correct 1 ms 1228 KB Output is correct
15 Correct 1 ms 1228 KB Output is correct
16 Correct 1 ms 1228 KB Output is correct
17 Correct 1 ms 1228 KB Output is correct
18 Correct 1 ms 1228 KB Output is correct
19 Correct 31 ms 3420 KB Output is correct
20 Correct 31 ms 3448 KB Output is correct
21 Correct 31 ms 3392 KB Output is correct
22 Correct 33 ms 3480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1996 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 6 ms 1996 KB Output is correct
4 Correct 8 ms 1996 KB Output is correct
5 Correct 2 ms 1356 KB Output is correct
6 Correct 2 ms 1868 KB Output is correct
7 Correct 1 ms 1228 KB Output is correct
8 Correct 1 ms 1228 KB Output is correct
9 Correct 1 ms 1356 KB Output is correct
10 Correct 81 ms 2016 KB Output is correct
11 Correct 111 ms 2044 KB Output is correct
12 Correct 129 ms 2028 KB Output is correct
13 Correct 3 ms 1996 KB Output is correct
14 Correct 8 ms 1996 KB Output is correct
15 Correct 5 ms 1996 KB Output is correct
16 Correct 8 ms 1996 KB Output is correct
17 Correct 35 ms 3040 KB Output is correct
18 Correct 37 ms 3112 KB Output is correct
19 Correct 40 ms 3060 KB Output is correct
20 Correct 6 ms 2020 KB Output is correct
21 Correct 10 ms 1996 KB Output is correct
22 Correct 3 ms 1996 KB Output is correct
23 Correct 2 ms 1868 KB Output is correct
24 Correct 1 ms 1228 KB Output is correct
25 Correct 31 ms 3148 KB Output is correct
26 Correct 29 ms 3244 KB Output is correct
27 Correct 37 ms 3268 KB Output is correct
28 Correct 36 ms 3264 KB Output is correct
29 Correct 38 ms 3204 KB Output is correct
30 Correct 34 ms 3184 KB Output is correct
31 Correct 9 ms 1996 KB Output is correct
32 Correct 2 ms 1868 KB Output is correct
33 Correct 27 ms 2952 KB Output is correct
34 Correct 2 ms 1996 KB Output is correct
35 Correct 30 ms 3340 KB Output is correct
36 Correct 1 ms 1228 KB Output is correct
37 Correct 1 ms 1228 KB Output is correct
38 Correct 30 ms 3360 KB Output is correct
39 Correct 29 ms 3396 KB Output is correct
40 Correct 30 ms 3484 KB Output is correct
41 Correct 33 ms 3404 KB Output is correct
42 Correct 31 ms 3392 KB Output is correct
43 Correct 1 ms 1228 KB Output is correct
44 Correct 1 ms 1228 KB Output is correct
45 Correct 1 ms 1228 KB Output is correct
46 Correct 1 ms 1228 KB Output is correct
47 Correct 1 ms 1228 KB Output is correct
48 Correct 1 ms 1228 KB Output is correct
49 Correct 31 ms 3420 KB Output is correct
50 Correct 31 ms 3448 KB Output is correct
51 Correct 31 ms 3392 KB Output is correct
52 Correct 33 ms 3480 KB Output is correct
53 Correct 35 ms 3440 KB Output is correct
54 Correct 39 ms 3544 KB Output is correct
55 Correct 37 ms 3524 KB Output is correct
56 Correct 6 ms 1956 KB Output is correct
57 Correct 6 ms 2032 KB Output is correct
58 Correct 177 ms 3272 KB Output is correct
59 Correct 191 ms 3276 KB Output is correct
60 Correct 276 ms 3280 KB Output is correct
61 Correct 167 ms 3280 KB Output is correct
62 Correct 189 ms 3276 KB Output is correct
63 Correct 253 ms 3276 KB Output is correct
64 Correct 134 ms 3276 KB Output is correct
65 Correct 144 ms 3356 KB Output is correct
66 Correct 183 ms 3264 KB Output is correct
67 Correct 21 ms 2636 KB Output is correct
68 Correct 35 ms 3404 KB Output is correct
69 Correct 32 ms 3408 KB Output is correct
70 Correct 38 ms 3448 KB Output is correct
71 Correct 39 ms 3616 KB Output is correct
72 Correct 35 ms 3396 KB Output is correct
73 Correct 37 ms 3432 KB Output is correct
74 Correct 36 ms 3396 KB Output is correct