Submission #1340204

#TimeUsernameProblemLanguageResultExecution timeMemory
1340204adfsadfRobot (JOI21_ho_t4)C++20
0 / 100
747 ms82040 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int inf = 1e18, N = 1e5 + 5;

struct edge {
    int to, c, p;
};

map<int, vector<edge>> graph[N];
vector<int> dp(N, inf);
map<int, int> dp2[N], psum[N];

int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v, c;
        int p;
        cin >> u >> v >> c >> p;
        graph[u][c].push_back({v, c, p});
        graph[v][c].push_back({u, c, p});
        psum[u][c] += p;
        psum[v][c] += p;
    }
    dp[1] = 0;
    priority_queue<tuple<int, int, int>> pq;
    pq.push({0, 1, 0});
    while (pq.size()) {
        int cost;
        int node, c;
        tie(cost, node, c) = pq.top();
        pq.pop();

        if (c) {
            if (dp2[node][c] != -cost) continue;
            for (edge i : graph[node][c]) {
                // We can't flip i in this case
                int case1 = psum[node][c] - i.p;
                if (case1 - cost < dp[i.to]) {
                    dp[i.to] = case1 - cost;
                    pq.push({-dp[i.to], i.to, 0});
                }
            }
        } else {
            if (dp[node] != -cost) continue;
            for (auto &i : graph[node]) {
                for (edge j : i.second) {
                    // Case 1: We don't flip j
                    int case1 = psum[node][j.c] - j.p - cost;
                    if (case1 < dp[j.to]) {
                        dp[j.to] = case1;
                        pq.push({-dp[j.to], j.to, 0});
                    }
                    // Case 2: We flip j but not another edge of the same colour
                    int case2 = j.p - cost;
                    if (case2 < dp[j.to]) {
                        dp[j.to] = case2;
                        pq.push({-dp[j.to], j.to, 0});
                    }
                    // Case 3: We flip j and another edge of the same colour
                    int case3 = -cost;
                    if (!dp2[j.to].count(j.c) || case3 < dp2[j.to][j.c]) {
                        dp2[j.to][j.c] = case3;
                        pq.push({-dp2[j.to][j.c], j.to, j.c});
                    }
                }
            }
        }
    }
    cout << (dp[n] > inf ? -1 : dp[n]);
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...