답안 #753336

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
753336 2023-06-05T05:24:40 Z Kihihihi Olympic Bus (JOI20_ho_t4) C++17
100 / 100
170 ms 7228 KB
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cassert>
#include <ctime>
#include <chrono>
#include <cstdio>
#include <random>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <deque>
#include <queue>
#include <bitset>
#include <list>
#include <fstream>
#include <functional>
#include <complex>
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());

short skip_cin = 0;
const long long INF = 1e17, MOD = 1e9 + 7, MOD2 = 998244353, LOG = 18;
const long double EPS = 1e-9, PI = acos(-1);

struct edge
{
    long long to, w, idx;
};

long long n;
vector<edge> g[200], rg[200];

void dijkstra(long long s, vector<long long>& dist, vector<pair<long long, long long>>& p, bool rev, long long del = -1)
{
    if (rev)
    {
        swap(g, rg);
    }

    dist[s] = 0;
    vector<bool> used(n);
    while (true)
    {
        long long v = -1;
        for (long long i = 0; i < n; i++)
        {
            if (!used[i] && dist[i] != INF && (v == -1 || dist[i] < dist[v]))
            {
                v = i;
            }
        }
        if (v == -1)
        {
            break;
        }

        used[v] = true;
        for (auto& i : g[v])
        {
            if (i.idx == del)
            {
                continue;
            }

            if (dist[v] + i.w < dist[i.to])
            {
                dist[i.to] = dist[v] + i.w;
                p[i.to] = { v, i.idx };
            }
        }
    }

    if (rev)
    {
        swap(g, rg);
    }
}

vector<long long> get_path(long long a, long long b, vector<pair<long long, long long>>& p)
{
    if (p[b].first == -1)
    {
        return {};
    }

    vector<long long> res;
    while (b != a)
    {
        res.push_back(p[b].second);
        b = p[b].first;
    }
    return res;
}

void solve()
{
    long long m;
    cin >> n >> m;
    vector<long long> rv(m), w(m);
    vector<pair<long long, long long>> frto(m);
    for (long long i = 0; i < m; i++)
    {
        long long a, b, c, d;
        cin >> a >> b >> c >> d;
        a--; b--;
        g[a].push_back({ b, c, i });
        rg[b].push_back({ a, c, i });
        rv[i] = d;
        frto[i] = { a, b };
        w[i] = c;
    }

    vector<long long> da(n, INF), db(n, INF), rda(n, INF), rdb(n, INF);
    vector<pair<long long, long long>> pa(n, { -1, -1 }), pb(n, { -1, -1 }), TRASH(n);
    dijkstra(0, da, pa, false);
    dijkstra(n - 1, db, pb, false);
    dijkstra(0, rda, TRASH, true);
    dijkstra(n - 1, rdb, TRASH, true);

    vector<long long> ea = get_path(0, n - 1, pa), eb = get_path(n - 1, 0, pb);
    vector<bool> used(m);
    for (auto& i : ea)
    {
        used[i] = true;
    }
    for (auto& i : eb)
    {
        used[i] = true;
    }

    long long ans = min(INF, da[n - 1] + db[0]);
    for (long long i = 0; i < m; i++)
    {
        auto [x, y] = frto[i];
        if (!used[i])
        {
            ans = min(ans, rv[i] + min(da[n - 1], da[y] + w[i] + rdb[x]) + min(db[0], db[y] + w[i] + rda[x]));
            continue;
        }

        g[y].push_back({ x, w[i], INF });
        vector<long long> fucka(n, INF), fuckb(n, INF);
        dijkstra(0, fucka, TRASH, false, i);
        dijkstra(n - 1, fuckb, TRASH, false, i);
        ans = min(ans, fucka[n - 1] + fuckb[0] + rv[i]);
        g[y].pop_back();
    }
    cout << (ans == INF ? -1 : ans) << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    srand(time(NULL));

    int tst = 1;
    //cin >> tst;
    while (tst--)
    {
        solve();
    }

    return 0;
}

/*
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠤⠖⠚⢉⣩⣭⡭⠛⠓⠲⠦⣄⡀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢀⡴⠋⠁⠀⠀⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠳⢦⡀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡴⠃⢀⡴⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀
⠀⠀⠀⠀⡾⠁⣠⠋⠀⠈⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧⠀⠀
⠀⠀⠀⣸⠁⢰⠃⠀⠀⠀⠈⢣⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣇⠀
⠀⠀⠀⡇⠀⡾⡀⠀⠀⠀⠀⣀⣹⣆⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⠀
⠀⠀⢸⠃⢀⣇⡈⠀⠀⠀⠀⠀⠀⢀⡑⢄⡀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⢸⠀⢻⡟⡻⢶⡆⠀⠀⠀⠀⡼⠟⡳⢿⣦⡑⢄⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⣸⠀⢸⠃⡇⢀⠇⠀⠀⠀⠀⠀⡼⠀⠀⠈⣿⡗⠂⠀⠀⠀⠀⠀⠀⠀⢸⠁
⠀⠀⡏⠀⣼⠀⢳⠊⠀⠀⠀⠀⠀⠀⠱⣀⣀⠔⣸⠁⠀⠀⠀⠀⠀⠀⠀⢠⡟⠀
⠀⠀⡇⢀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⢸⠃⠀
⠀⢸⠃⠘⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠁⠀⠀⢀⠀⠀⠀⠀⠀⣾⠀⠀
⠀⣸⠀⠀⠹⡄⠀⠀⠈⠁⠀⠀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠸⠀⠀⠀⠀⠀⡇⠀⠀
⠀⡏⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⢀⣠⢶⡇⠀⠀⢰⡀⠀⠀⠀⠀⠀⡇⠀⠀
⢰⠇⡄⠀⠀⠀⡿⢣⣀⣀⣀⡤⠴⡞⠉⠀⢸⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⣧⠀⠀
⣸⠀⡇⠀⠀⠀⠀⠀⠀⠉⠀⠀⠀⢹⠀⠀⢸⠀⠀⢀⣿⠇⠀⠀⠀⠁⠀⢸⠀⠀
⣿⠀⡇⠀⠀⠀⠀⠀⢀⡤⠤⠶⠶⠾⠤⠄⢸⠀⡀⠸⣿⣀⠀⠀⠀⠀⠀⠈⣇⠀
⡇⠀⡇⠀⠀⡀⠀⡴⠋⠀⠀⠀⠀⠀⠀⠀⠸⡌⣵⡀⢳⡇⠀⠀⠀⠀⠀⠀⢹⡀
⡇⠀⠇⠀⠀⡇⡸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠮⢧⣀⣻⢂⠀⠀⠀⠀⠀⠀⢧
⣇⠀⢠⠀⠀⢳⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡎⣆⠀⠀⠀⠀⠀⠘
⢻⠀⠈⠰⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⠘⢮⣧⡀⠀⠀⠀⠀
⠸⡆⠀⠀⠇⣾⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠆⠀⠀⠀⠀⠀⠀⠀⠙⠳⣄⡀⢢⡀
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 468 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 4 ms 412 KB Output is correct
4 Correct 6 ms 476 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 44 ms 468 KB Output is correct
11 Correct 57 ms 488 KB Output is correct
12 Correct 62 ms 472 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 4 ms 468 KB Output is correct
15 Correct 3 ms 468 KB Output is correct
16 Correct 3 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 6636 KB Output is correct
2 Correct 23 ms 6788 KB Output is correct
3 Correct 25 ms 7056 KB Output is correct
4 Correct 3 ms 588 KB Output is correct
5 Correct 4 ms 536 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 23 ms 6956 KB Output is correct
10 Correct 19 ms 6864 KB Output is correct
11 Correct 22 ms 6628 KB Output is correct
12 Correct 22 ms 6484 KB Output is correct
13 Correct 22 ms 6940 KB Output is correct
14 Correct 21 ms 7228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 24 ms 4864 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 20 ms 6532 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 19 ms 6544 KB Output is correct
9 Correct 21 ms 6604 KB Output is correct
10 Correct 20 ms 6612 KB Output is correct
11 Correct 20 ms 6692 KB Output is correct
12 Correct 20 ms 6800 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 19 ms 6788 KB Output is correct
20 Correct 21 ms 6604 KB Output is correct
21 Correct 20 ms 6660 KB Output is correct
22 Correct 19 ms 6304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 468 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 4 ms 412 KB Output is correct
4 Correct 6 ms 476 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 44 ms 468 KB Output is correct
11 Correct 57 ms 488 KB Output is correct
12 Correct 62 ms 472 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 4 ms 468 KB Output is correct
15 Correct 3 ms 468 KB Output is correct
16 Correct 3 ms 468 KB Output is correct
17 Correct 22 ms 6636 KB Output is correct
18 Correct 23 ms 6788 KB Output is correct
19 Correct 25 ms 7056 KB Output is correct
20 Correct 3 ms 588 KB Output is correct
21 Correct 4 ms 536 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 23 ms 6956 KB Output is correct
26 Correct 19 ms 6864 KB Output is correct
27 Correct 22 ms 6628 KB Output is correct
28 Correct 22 ms 6484 KB Output is correct
29 Correct 22 ms 6940 KB Output is correct
30 Correct 21 ms 7228 KB Output is correct
31 Correct 3 ms 468 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 24 ms 4864 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 20 ms 6532 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 19 ms 6544 KB Output is correct
39 Correct 21 ms 6604 KB Output is correct
40 Correct 20 ms 6612 KB Output is correct
41 Correct 20 ms 6692 KB Output is correct
42 Correct 20 ms 6800 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 328 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 19 ms 6788 KB Output is correct
50 Correct 21 ms 6604 KB Output is correct
51 Correct 20 ms 6660 KB Output is correct
52 Correct 19 ms 6304 KB Output is correct
53 Correct 23 ms 6552 KB Output is correct
54 Correct 23 ms 6228 KB Output is correct
55 Correct 23 ms 6392 KB Output is correct
56 Correct 3 ms 468 KB Output is correct
57 Correct 3 ms 468 KB Output is correct
58 Correct 82 ms 5048 KB Output is correct
59 Correct 93 ms 5136 KB Output is correct
60 Correct 121 ms 5104 KB Output is correct
61 Correct 77 ms 5100 KB Output is correct
62 Correct 88 ms 5160 KB Output is correct
63 Correct 114 ms 5076 KB Output is correct
64 Correct 125 ms 5472 KB Output is correct
65 Correct 134 ms 5468 KB Output is correct
66 Correct 170 ms 5452 KB Output is correct
67 Correct 16 ms 4820 KB Output is correct
68 Correct 25 ms 6796 KB Output is correct
69 Correct 20 ms 6752 KB Output is correct
70 Correct 22 ms 6480 KB Output is correct
71 Correct 22 ms 6604 KB Output is correct
72 Correct 22 ms 6748 KB Output is correct
73 Correct 22 ms 7036 KB Output is correct
74 Correct 27 ms 6996 KB Output is correct