Submission #623332

#TimeUsernameProblemLanguageResultExecution timeMemory
623332BhavayGoyalRobot (JOI21_ho_t4)C++14
24 / 100
1014 ms81016 KiB
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<class T> using oset = 
            tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"

const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;

const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};


// -------------------------------------------------- Main Code --------------------------------------------------

void sol() {
    int n, m; cin >> n >> m;
    map<int, vector<array<int, 3>>> g[n];
    map<int, int> vis[n], pref[n];
    for (int i = 0; i < m; i++) {
        int a, b, c, d; cin >> a >> b >> c >> d; --a; --b;
        g[a][c].pb({b, c, d});
        g[b][c].pb({a, c, d});
        pref[a][c] += d;
        pref[b][c] += d;
    }

    priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int, 3>>> q;
    q.push({0, 0, 0}); // dis, src, colour
    while (!q.empty()) {
        auto temp = q.top(); q.pop();
        int dist = temp[0], src = temp[1], col = temp[2];
        if (vis[src][col]) continue;
        vis[src][col] = true;
        if (src == n-1 && !col) {
            cout << dist << endl;
            return;
        }

        if (col) {
            for (auto ch : g[src][col]) {
                int child = ch[0], chCol = ch[1], chp = ch[2];
                q.push({pref[src][chCol] - chp + dist, child, 0});
            }
        }
        else {
            for (auto chi : g[src]) {
                for (auto ch : chi.s) {
                    int child = ch[0], chCol = ch[1], chp = ch[2];
                    q.push({min(pref[src][chCol] - chp + dist, chp + dist), child, 0});
                    q.push({dist, child, chCol});
                }
            }
        }
    }
    cout << -1 << endl;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1;
    // cin >> t; 
    while (t--) {
        sol();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...