This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
//#define DEBUG
void setIO(string name) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin.exceptions(istream::failbit);
#ifdef LOCAL
    freopen((name + ".in").c_str(), "r", stdin);
    freopen((name + ".out").c_str(), "w", stdout);
    freopen((name + ".out").c_str(), "w", stderr);
#endif
}
const int maxn = 1e5 + 5;
const long long inf = 1e18;
struct edge {
    long long e, c, p;
};
vector<edge> adj[maxn];
long long dist[maxn];
int main() {
    setIO("1");
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int a, b, c, p;
        cin >> a >> b >> c >> p;
        --a, --b;
        adj[a].push_back({b, c, p});
        adj[b].push_back({a, c, p});
    }
    for (int i = 0; i < n; ++i) {
        dist[i] = inf;
    }
    dist[0] = 0;
    priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> q;
    q.push({0, 0});
    while (!q.empty()) {
        map<int, pair<int, long long>> colors;
        pair<long long, int> cur = q.top();
        q.pop();
        long long node = cur.second, cost = cur.first;
        if (dist[node] != cost) {
            continue;
        }
        for (auto i : adj[node]) {
            ++colors[i.c].first;
            colors[i.c].second += i.p;
        }
        for (auto i : adj[node]) {
            if (colors[i.c].first == 1) {
                if (dist[i.e] > cost) {
#ifdef DEBUG
                    cout << node << " " << i.e << " " << cost << "\n";
#endif
                    q.push({dist[i.e] = cost, i.e});
                }
            } else {
                long long nc = cost + min(i.p, colors[i.c].second - i.p);
                if (dist[i.e] > nc) {
#ifdef DEBUG
                    cout << node << " " << i.e << " " << cost << " " << nc << "\n";
#endif
                    q.push({dist[i.e] = nc, i.e});
                }
            }
        }
    }
#ifdef DEBUG
    for (int i = 0; i < n; ++i) {
        cout << dist[i] << " ";
    }
    cout << "\n";
#endif
    cout << (dist[n - 1] == inf ? -1 : dist[n - 1]) << "\n";
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |