Submission #922947

# Submission time Handle Problem Language Result Execution time Memory
922947 2024-02-06T10:30:07 Z vjudge1 Olympic Bus (JOI20_ho_t4) C++17
11 / 100
1000 ms 4632 KB
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#include <x86intrin.h>

#include <bits/stdc++.h>
#include <chrono>
#include <random>

// @author: Vlapos

namespace operators
{
    template <typename T1, typename T2>
    std::istream &operator>>(std::istream &in, std::pair<T1, T2> &x)
    {
        in >> x.first >> x.second;
        return in;
    }

    template <typename T1, typename T2>
    std::ostream &operator<<(std::ostream &out, std::pair<T1, T2> x)
    {
        out << x.first << " " << x.second;
        return out;
    }

    template <typename T1>
    std::istream &operator>>(std::istream &in, std::vector<T1> &x)
    {
        for (auto &i : x)
            in >> i;
        return in;
    }

    template <typename T1>
    std::ostream &operator<<(std::ostream &out, std::vector<T1> &x)
    {
        for (auto &i : x)
            out << i << " ";
        return out;
    }

    template <typename T1>
    std::ostream &operator<<(std::ostream &out, std::set<T1> &x)
    {
        for (auto &i : x)
            out << i << " ";
        return out;
    }
}

// name spaces
using namespace std;
using namespace operators;
// end of name spaces

// defines
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second
#define uint unsigned int
#define all(vc) vc.begin(), vc.end()
// end of defines

// usefull stuff

void boost()
{
    ios_base ::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

inline int getbit(int &x, int &bt) { return (x >> bt) & 1; }

const int dx4[4] = {-1, 0, 0, 1};
const int dy4[4] = {0, -1, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy8[8] = {-1, -0, 1, -1, 1, -1, 0, 1};

const ll INF = (1e15) + 500;
const int BIG = (2e9) + 500;
const int MOD7 = (1e9) + 7;
const int MOD9 = (1e9) + 9;
const uint MODFFT = 998244353;

#define int ll

struct test
{
    vector<vector<pll>> graph, invgraph;
    vector<int> components;
    int n, m;

    void merge(pll &a, ll val)
    {
        if (a.f > val)
        {
            a.s = a.f;
            a.f = val;
        }
        else
            a.s = min(a.s, val);
    }

    vector<int> used;
    void dijkstra(vector<ll> &dists, int st, int U, int V, ll C)
    {
        for (int i = 0; i < n; ++i)
            dists[i] = INF, used[i] = 0;
        dists[st] = 0;
        for (int i = 0; i < n; ++i)
        {
            int v = -1;
            for (int i = 0; i < n; ++i)
                if (!used[i] and (v == -1 or dists[v] > dists[i]))
                    v = i;
            if (v == -1)
                break;
            used[v] = 1;
            for (int i = 0; i < n; ++i)
                if (!used[i])
                {
                    ll cost;
                    if (U == v and V == i and graph[v][i].f == C)
                        cost = graph[v][i].s;
                    else
                        cost = graph[v][i].f;

                    if (U == i and V == v)
                        cost = min(cost, C);

                    dists[i] = min(dists[i], dists[v] + cost);
                }
        }
    }

    void invdijkstra(vector<ll> &dists, int st, int U, int V, ll C)
    {
        for (int i = 0; i < n; ++i)
            dists[i] = INF, used[i] = 0;
        dists[st] = 0;
        for (int i = 0; i < n; ++i)
        {
            int v = -1;
            for (int i = 0; i < n; ++i)
                if (!used[i] and (v == -1 or dists[v] > dists[i]))
                    v = i;
            if (v == -1)
                break;
            used[v] = 1;
            for (int i = 0; i < n; ++i)
                if (!used[i])
                {
                    ll cost = (U == v and V == i and invgraph[v][i].f == C ? invgraph[v][i].s : invgraph[v][i].f);
                    dists[i] = min(dists[i], dists[v] + cost);
                }
        }
    }

    void solve(int testcase)
    {
        boost();

        cin >> n >> m;

        used.resize(n);
        graph.resize(n, vector<pll>(n, {INF, INF}));
        invgraph.resize(n, vector<pll>(n, {INF, INF}));

        vector<pair<pii, pii>> edges;

        for (int i = 0; i < m; ++i)
        {
            int v, tov, c, d;
            cin >> v >> tov >> c >> d;
            --v, --tov;
            merge(graph[v][tov], c);
            merge(invgraph[tov][v], c);
            edges.pb({{d, c}, {v, tov}});
        }
        sort(all(edges));

        vector<ll> dist0(n, INF), distN(n, INF), to0(n, INF), toN(n, INF);

        dijkstra(dist0, 0, 0, 0, 0);
        dijkstra(distN, n - 1, 0, 0, 0);
        invdijkstra(to0, 0, 0, 0, 0);
        invdijkstra(toN, n - 1, 0, 0, 0);

        vector<ll> bf(n);

        ll res = INF;
        if (dist0[n - 1] == INF and distN[n - 1] == INF)
        {
            for (auto [e1, e2] : edges)
            {
                auto [v, tov] = e2;
                auto [d, c] = e1;
                res = min(res, d + dist0[tov] + c + toN[v] + distN[tov] + c + to0[v]);
            }
        }
        else
        {
            map<pair<pii, int>, ll> bad;

            if (dist0[n - 1] != INF)
            {
                int v = 0;
                while (v != n - 1)
                {
                    for (int tov = 0; tov < n; ++tov)
                        if (toN[v] == toN[tov] + graph[v][tov].f)
                        {
                            ll curRes = 0;
                            dijkstra(bf, 0, v, tov, graph[v][tov].f);
                            curRes += bf[n - 1];
                            dijkstra(bf, n - 1, v, tov, graph[v][tov].f);
                            curRes += bf[0];
                            bad[{{v, tov}, graph[v][tov].f}] = curRes;

                            v = tov;
                            continue;
                        }
                }
            }
            if (distN[0] != INF)
            {
                int v = n - 1;
                while (v != 0)
                {
                    for (int tov = 0; tov < n; ++tov)
                        if (to0[v] == to0[tov] + graph[v][tov].f)
                        {
                            ll curRes = 0;
                            dijkstra(bf, 0, v, tov, graph[v][tov].f);
                            curRes += bf[n - 1];
                            dijkstra(bf, n - 1, v, tov, graph[v][tov].f);
                            curRes += bf[0];
                            bad[{{v, tov}, graph[v][tov].f}] = curRes;

                            v = tov;
                            continue;
                        }
                }
            }
            int way0N = dist0[n - 1];
            int wayN0 = distN[0];
            res = min(res, way0N + wayN0);

            for (auto [e1, e2] : edges)
            {
                auto [v, tov] = e2;
                auto [d, c] = e1;
                if (bad.find({{v, tov}, c}) != bad.end())
                {
                    res = min(res, d + bad[{{v, tov}, c}]);
                }
                else
                {
                    res = min(res, d + min(way0N, dist0[tov] + c + toN[v]) + min(wayN0, distN[tov] + c + to0[v]));
                }
            }
        }

        if (res == INF)
            cout << "-1\n";
        else
            cout << res << "\n";
    }
};

main()
{
    boost();
    int q = 1;
    // cin >> q;
    for (int i = 0; i < q; i++)
    {
        test t;
        t.solve(i);
    }
    return 0;
}
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//
//                                                                                    //
//                               Coded by Der_Vlἀpos                                  //
//                                                                                    //
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//

Compilation message

ho_t4.cpp:278:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  278 | main()
      | ^~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 1067 ms 1628 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 21 ms 4560 KB Output is correct
2 Correct 25 ms 4456 KB Output is correct
3 Correct 21 ms 4556 KB Output is correct
4 Correct 4 ms 1884 KB Output is correct
5 Correct 6 ms 1628 KB Output is correct
6 Correct 2 ms 1628 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 18 ms 4556 KB Output is correct
10 Correct 18 ms 4556 KB Output is correct
11 Correct 22 ms 4556 KB Output is correct
12 Correct 21 ms 4632 KB Output is correct
13 Correct 22 ms 4560 KB Output is correct
14 Correct 22 ms 4560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1043 ms 1628 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1067 ms 1628 KB Time limit exceeded
2 Halted 0 ms 0 KB -