Submission #1320436

#TimeUsernameProblemLanguageResultExecution timeMemory
1320436Vu_CG_CoderRobot (JOI21_ho_t4)C++20
100 / 100
255 ms45716 KiB
#include <bits/stdc++.h>
#define fore(i, a, b) for (int i = (a), i##_last = (b); i < i##_last; ++i)
#define fort(i, a, b) for (int i = (a), i##_last = (b); i <= i##_last; ++i)
#define ford(i, a, b) for (int i = (a), i##_last = (b); i >= i##_last; --i)
#define fi first
#define se second
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<class A, class B> bool maxi(A &a, const B &b) {return (a < b) ? (a = b, true): false;};
template<class A, class B> bool mini(A &a, const B &b) {return (a > b) ? (a = b, true): false;};

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

template<class A, class B> ostream& operator << (ostream &outputStream, const pair<A, B> &p) {
    return outputStream << '{' << p.fi << ", " << p.se << '}';
}

template<class T> ostream& operator << (ostream &outputStream, const vector<T> &v) {
    outputStream << '{';
    for (const T &t : v)
        outputStream << t << ' ';
    return outputStream << '}';
}

constexpr ll INF = 0X3F3F3F3F3F3F3F3F;
constexpr int MAX_S = 1E5 + 5;

unordered_map<int, tuple<ll, ll, vii> > g[MAX_S];
ll f[MAX_S];
int S, K;

void input() {
    int u = 0, v = 0, x = 0, w = 0;

    cin >> S >> K;

    fort(i, 2, S)
        f[i] = INF;

    fort(_, 1, K) {
        cin >> u >> v >> x >> w;
        auto &[fu, su, eu] = g[u][x];
        auto &[fv, sv, ev] = g[v][x];
        fu = fv = INF;
        su += w;
        sv += w;
        eu.emplace_back(w, v);
        ev.emplace_back(w, u);
    }
}

void solve() {
    priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int> >, greater<tuple<ll, int, int> >  > heap;

    heap.emplace(0, 1, 0);

    for (; !heap.empty();) {
        const auto [w, u, c] = heap.top();
        heap.pop();

        if (c) {
            auto &[fu, su, eu] = g[u][c];
            if (fu != w)
                continue;
            for (const auto &[w, v] : eu)
                if (mini(f[v], su - w + fu))
                    heap.emplace(f[v], v, 0);
        } else {
            if (f[u] != w)
                continue;
            for (const auto &[c, e] : g[u]) {
                const auto &[fu, su, eu] = e;
                for (const auto &[w, v] : eu) {
                    auto &[fv, sv, ev] = g[v][c];
                    if (mini(f[v], min((ll)w, su - w) + f[u]))
                        heap.emplace(f[v], v, 0);
                    if (mini(fv, f[u]))
                        heap.emplace(fv, v, c);
                }
            }
        }
    }

    cout << ((f[S] < INF) ? f[S] : (-1)) << '\n';
}

int main() {
    #ifdef LOCAL
        freopen("input.INP", "r", stdin);
    #endif // LOCAL
    cin.tie(0) -> sync_with_stdio(0);
    cout.tie(0);

    input();
    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...