Submission #923139

# Submission time Handle Problem Language Result Execution time Memory
923139 2024-02-06T17:58:12 Z Camillus Olympic Bus (JOI20_ho_t4) C++17
100 / 100
787 ms 6504 KB
/// @author Camillus
#include <bits/stdc++.h>
#define int long long

using ll = long long;
using namespace std;

int from[50'000];
int to[50'000];
int cost[50'000];
int change[50'000];

static constexpr int INF = 1e17;

int n, m;

vector<pair<int, int>> g[200];
int d_from[200];
int d_to[200];

vector<int> shuffled;

void dist_from_all(int s) {
    for (int u = 0; u < n; u++) {
        g[u].resize(0);
    }

    for (int i : shuffled) {
        g[to[i]].emplace_back(from[i], cost[i]);
    }

    auto d = d_to;
    for (int i = 0; i < n; i++) {
        d[i] = INF;
    }
    d[s] = 0;

    set<pair<int, int>> Q;
    Q.emplace(d[s], s);

    while (!Q.empty()) {
        int u = Q.begin()->second;
        Q.erase(Q.begin());

        for (auto [v, c] : g[u]) {
            if (d[v] > d[u] + c) {
                Q.erase(pair(d[v], v));
                d[v] = d[u] + c;
                Q.emplace(d[v], v);
            }
        }
    }
}

void dist_to_all(int s) {
    for (int u = 0; u < n; u++) {
        g[u].resize(0);
    }

    for (int i : shuffled) {
        g[from[i]].emplace_back(to[i], cost[i]);
    }

    auto d = d_from;
    for (int i = 0; i < n; i++) {
        d[i] = INF;
    }
    d[s] = 0;

    set<pair<int, int>> Q;
    Q.emplace(d[s], s);

    while (!Q.empty()) {
        int u = Q.begin()->second;
        Q.erase(Q.begin());

        for (auto [v, c] : g[u]) {
            if (d[v] > d[u] + c) {
                Q.erase(pair(d[v], v));
                d[v] = d[u] + c;
                Q.emplace(d[v], v);
            }
        }
    }
}

int get_cost() {
    int result = 0;

    dist_to_all(0);
    dist_from_all(0);

    return min(INF, d_from[n - 1] + d_to[n - 1]);
}

random_device rd;
mt19937_64 rnd(rd());

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m;

    shuffled.resize(m);
    iota(shuffled.begin(), shuffled.end(), 0);
    shuffle(shuffled.begin(), shuffled.end(), rnd);

    for (int i = 0; i < m; i++) {
        int u, v, c, d;
        cin >> u >> v >> c >> d;
        u--, v--;

        from[i] = u;
        to[i] = v;
        cost[i] = c;
        change[i] = d;
    }

    auto group1 = [&]() -> bool {
        return m <= 1000;
    };

    auto group2 = [&]() -> bool {
        if (m % 2 == 1) {
            return false;
        }

        for (int i = 0; i < m; i += 2) {
            bool condition = from[i] == from[i + 1] && to[i] == to[i + 1] && cost[i] == cost[i + 1];
            if (!condition) {
                return false;
            }
        }

        return true;
    };

    auto group3 = [&]() -> bool {
        for (int i = 0; i < m; i++) {
            if (cost[i] != 0) {
                return false;
            }
        }
        return true;
    };

    vector<vector<int>> d(n, vector<int>(n, INF));
    vector<int> good;

    for (int i = 0; i < m; i++) {
        d[from[i]][to[i]] = min(d[from[i]][to[i]], cost[i]);
    }

    for (int u = 0; u < n; u++) {
        d[u][u] = 0;
    }

    for (int k = 0; k < n; k++) {
        for (int u = 0; u < n; u++) {
            for (int v = 0; v < n; v++) {
                d[u][v] = min(d[u][v], d[u][k] + d[k][v]);
            }
        }
    }

    vector<int> best1(m);
    iota(best1.begin(), best1.end(), 0);
    sort(best1.begin(), best1.end(), [&](int i, int j) -> bool {
        auto val = [&](int x) -> int {
            return d[0][to[x]] + cost[x] + change[x] + d[from[x]][n - 1];
        };
        return val(i) < val(j);
    });
    best1.resize(min(best1.size(), (size_t)100));

    vector<int> best2(m);
    iota(best2.begin(), best2.end(), 0);
    sort(best2.begin(), best2.end(), [&](int i, int j) -> bool {
        auto val = [&](int x) -> int {
            return d[n - 1][to[x]] + cost[x] + change[x] + d[from[x]][0];
        };
        return val(i) < val(j);
    });
    best2.resize(min(best2.size(), (size_t)100));

    vector<int> best;
    for (int i : best1) {
        best.push_back(i);
    }
    for (int i : best2) {
        best.push_back(i);
    }

    sort(best.begin(), best.end());
    best.erase(unique(best.begin(), best.end()), best.end());

    int initial = get_cost();
    int ans = initial;

    for (int i : best) {
        swap(from[i], to[i]);
        ans = min(ans, get_cost() + change[i]);
        swap(from[i], to[i]);
    }

    cout << (ans == INF ? -1 : ans) << '\n';
    return 0;
}

Compilation message

ho_t4.cpp: In function 'long long int get_cost()':
ho_t4.cpp:88:9: warning: unused variable 'result' [-Wunused-variable]
   88 |     int result = 0;
      |         ^~~~~~
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:119:10: warning: variable 'group1' set but not used [-Wunused-but-set-variable]
  119 |     auto group1 = [&]() -> bool {
      |          ^~~~~~
ho_t4.cpp:123:10: warning: variable 'group2' set but not used [-Wunused-but-set-variable]
  123 |     auto group2 = [&]() -> bool {
      |          ^~~~~~
ho_t4.cpp:138:10: warning: variable 'group3' set but not used [-Wunused-but-set-variable]
  138 |     auto group3 = [&]() -> bool {
      |          ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 856 KB Output is correct
2 Correct 9 ms 824 KB Output is correct
3 Correct 16 ms 860 KB Output is correct
4 Correct 17 ms 860 KB Output is correct
5 Correct 4 ms 348 KB Output is correct
6 Correct 9 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 2 ms 604 KB Output is correct
10 Correct 20 ms 856 KB Output is correct
11 Correct 28 ms 1084 KB Output is correct
12 Correct 21 ms 860 KB Output is correct
13 Correct 15 ms 860 KB Output is correct
14 Correct 14 ms 916 KB Output is correct
15 Correct 13 ms 852 KB Output is correct
16 Correct 14 ms 1016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 149 ms 5076 KB Output is correct
2 Correct 196 ms 4948 KB Output is correct
3 Correct 127 ms 5040 KB Output is correct
4 Correct 22 ms 860 KB Output is correct
5 Correct 17 ms 856 KB Output is correct
6 Correct 10 ms 860 KB Output is correct
7 Correct 9 ms 604 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 125 ms 5312 KB Output is correct
10 Correct 123 ms 5356 KB Output is correct
11 Correct 135 ms 5200 KB Output is correct
12 Correct 118 ms 4944 KB Output is correct
13 Correct 119 ms 5040 KB Output is correct
14 Correct 115 ms 5056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 856 KB Output is correct
2 Correct 9 ms 604 KB Output is correct
3 Correct 82 ms 3892 KB Output is correct
4 Correct 9 ms 604 KB Output is correct
5 Correct 110 ms 5024 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 106 ms 5200 KB Output is correct
9 Correct 115 ms 5352 KB Output is correct
10 Correct 106 ms 5028 KB Output is correct
11 Correct 98 ms 5068 KB Output is correct
12 Correct 99 ms 4928 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 600 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 99 ms 4956 KB Output is correct
20 Correct 101 ms 5040 KB Output is correct
21 Correct 112 ms 5048 KB Output is correct
22 Correct 97 ms 5040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 856 KB Output is correct
2 Correct 9 ms 824 KB Output is correct
3 Correct 16 ms 860 KB Output is correct
4 Correct 17 ms 860 KB Output is correct
5 Correct 4 ms 348 KB Output is correct
6 Correct 9 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 2 ms 604 KB Output is correct
10 Correct 20 ms 856 KB Output is correct
11 Correct 28 ms 1084 KB Output is correct
12 Correct 21 ms 860 KB Output is correct
13 Correct 15 ms 860 KB Output is correct
14 Correct 14 ms 916 KB Output is correct
15 Correct 13 ms 852 KB Output is correct
16 Correct 14 ms 1016 KB Output is correct
17 Correct 149 ms 5076 KB Output is correct
18 Correct 196 ms 4948 KB Output is correct
19 Correct 127 ms 5040 KB Output is correct
20 Correct 22 ms 860 KB Output is correct
21 Correct 17 ms 856 KB Output is correct
22 Correct 10 ms 860 KB Output is correct
23 Correct 9 ms 604 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 125 ms 5312 KB Output is correct
26 Correct 123 ms 5356 KB Output is correct
27 Correct 135 ms 5200 KB Output is correct
28 Correct 118 ms 4944 KB Output is correct
29 Correct 119 ms 5040 KB Output is correct
30 Correct 115 ms 5056 KB Output is correct
31 Correct 13 ms 856 KB Output is correct
32 Correct 9 ms 604 KB Output is correct
33 Correct 82 ms 3892 KB Output is correct
34 Correct 9 ms 604 KB Output is correct
35 Correct 110 ms 5024 KB Output is correct
36 Correct 0 ms 344 KB Output is correct
37 Correct 0 ms 344 KB Output is correct
38 Correct 106 ms 5200 KB Output is correct
39 Correct 115 ms 5352 KB Output is correct
40 Correct 106 ms 5028 KB Output is correct
41 Correct 98 ms 5068 KB Output is correct
42 Correct 99 ms 4928 KB Output is correct
43 Correct 0 ms 344 KB Output is correct
44 Correct 0 ms 600 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 99 ms 4956 KB Output is correct
50 Correct 101 ms 5040 KB Output is correct
51 Correct 112 ms 5048 KB Output is correct
52 Correct 97 ms 5040 KB Output is correct
53 Correct 159 ms 4996 KB Output is correct
54 Correct 186 ms 4944 KB Output is correct
55 Correct 164 ms 4948 KB Output is correct
56 Correct 17 ms 860 KB Output is correct
57 Correct 17 ms 852 KB Output is correct
58 Correct 176 ms 3948 KB Output is correct
59 Correct 141 ms 3948 KB Output is correct
60 Correct 181 ms 3764 KB Output is correct
61 Correct 172 ms 3944 KB Output is correct
62 Correct 145 ms 3920 KB Output is correct
63 Correct 197 ms 3932 KB Output is correct
64 Correct 787 ms 4540 KB Output is correct
65 Correct 629 ms 5372 KB Output is correct
66 Correct 728 ms 5400 KB Output is correct
67 Correct 93 ms 5628 KB Output is correct
68 Correct 130 ms 6504 KB Output is correct
69 Correct 166 ms 6220 KB Output is correct
70 Correct 140 ms 6180 KB Output is correct
71 Correct 128 ms 6200 KB Output is correct
72 Correct 135 ms 6200 KB Output is correct
73 Correct 140 ms 6152 KB Output is correct
74 Correct 137 ms 6236 KB Output is correct