Submission #593532

#TimeUsernameProblemLanguageResultExecution timeMemory
593532piOOERobot (JOI21_ho_t4)C++17
100 / 100
753 ms66092 KiB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 100000;

map<int, vector<pair<int, int>>> g[N];
map<int, ll> dist[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < m; ++i) {
        int a, b, c, w;
        cin >> a >> b >> c >> w;
        --a, --b;
        g[a][c].emplace_back(b, w);
        g[b][c].emplace_back(a, w);
    }

    priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>, greater<>> pq;

    auto relax = [&pq](int v, int c, ll d) {
        if (!dist[v].count(c) || dist[v][c] > d) {
            dist[v][c] = d;
            pq.push({d, v, c});
        }
    };

    dist[0][0] = 0;
    pq.push({0, 0, 0});

    //we create c dummy vertices, v_0 is main one, v_c is for cases when we come to v with color c, and go from color c

    while (!pq.empty()) {
        auto [d, v, i] = pq.top();
        pq.pop();

        if (dist[v][i] < d) {
            continue;
        }

        if (i == 0) {
            for (auto [c, adj] : g[v]) {
                ll sum = 0;
                for (auto [to, w] : adj) {
                    sum += w;
                }

                for (auto [to, w] : adj) {
                    relax(to, 0, d + min((ll)w, sum - w));
                    relax(to, c, d);
                }
            }
        } else {
            ll sum = 0;
            for (auto [to, w] : g[v][i]) {
                sum += w;
            }

            for (auto [to, w] : g[v][i]) {
                relax(to, 0, d + sum - w);
            }
        }
    }

    cout << (dist[n - 1].count(0) ? dist[n - 1][0] : -1);

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