제출 #982885

#제출 시각아이디문제언어결과실행 시간메모리
982885aaaaaarrozRobot (JOI21_ho_t4)C++17
100 / 100
787 ms84548 KiB
#include <bits/stdc++.h> 
using namespace std; 
typedef long long ll; 
struct Edge {
    int to;
    int c;
    ll p;
};
const int N = 1e5 + 7;
const ll INF = 1e18;
     
int n, m;
map<int, vector<Edge>> g[N];
ll dp[N];
map<int, ll> dp2[N], psum[N];
     
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int a, b, c, p;
        cin >> a >> b >> c >> p;
        g[a][c].push_back({b, c, p});
        g[b][c].push_back({a, c, p});
        psum[a][c] += p;
        psum[b][c] += p;
    }
    for (int i = 1; i <= n; ++i) {
        dp[i] = INF;
    }
    priority_queue<tuple<ll, int, int>> pq;
    dp[1] = 0;
    pq.push({0, 1, 0});
    while (!pq.empty()) {
        ll cost;
        int u, c;
        tie(cost, u, c) = pq.top();
        pq.pop();
        if (c) {
            if (dp2[u][c] != -cost) {
                continue;
            }
            for (auto v : g[u][c]) {
                ll relax = psum[u][c] - v.p;
                if (relax - cost < dp[v.to]) {
                    dp[v.to] = relax - cost;
                    pq.push({-dp[v.to], v.to, 0});
                }
            }
        } 
        else {
            if (dp[u] != -cost) {
                continue;
            }
            for (auto v : g[u]) {
                for (auto edg : v.second) {
                    ll relax = psum[u][edg.c] - edg.p - cost;
                    if (relax < dp[edg.to]) {
                        dp[edg.to] = relax;
                        pq.push({-dp[edg.to], edg.to, 0});
                    }
                    relax = edg.p - cost;
                    if (relax < dp[edg.to]) {
                        dp[edg.to] = relax;
                        pq.push({-dp[edg.to], edg.to, 0});
                    }
                    relax = -cost;
                    if (!dp2[edg.to].count(edg.c) || relax < dp2[edg.to][edg.c]) {
                        dp2[edg.to][edg.c] = relax;
                        pq.push({-dp2[edg.to][edg.c], edg.to, edg.c});
                    }
                }
            }
        }
    }
    if (dp[n] == INF) {
        dp[n] = -1;
    }
    cout << dp[n];
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...