제출 #1340205

#제출 시각아이디문제언어결과실행 시간메모리
1340205adfsadfRobot (JOI21_ho_t4)C++20
0 / 100
1317 ms101212 KiB
#include <bits/stdc++.h>

#define int long long
using namespace std;

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

bool operator < (edge a, edge b) {
    if(a.to != b.to)
        return a.to < b.to;
    if(a.c != b.c)
        return a.c < b.c;
    return a.p < b.p;
}

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

unordered_map<int, vector<edge>> g[N];
unordered_map<int, int> dp[N], psum[N];

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v, c, p;
        cin >> u >> v >> c >> p;
        g[u][c].push_back({v, c, p});
        g[v][c].push_back({u, c, p});
        psum[u][c] += p;
        psum[v][c] += p;
    }
    dp[1][0] = 0;
    priority_queue<edge> pq;
    pq.push({0, 1, 0});
    while (!pq.empty()) {
        auto [cost, node, c] = pq.top();
        cost = -cost;
        pq.pop();
        if (c) {
            if (dp[node][c] != cost) continue;
            for (edge i: g[node][c]) {
                // We can't flip i in this case
                int case0 = psum[node][c] - i.p;
                if (!dp[i.to].count(0) || case0 + cost < dp[i.to][0]) {
                    dp[i.to][0] = case0 + cost;
                    pq.push({-dp[i.to][0], i.to, 0});
                }
            }
        } else {
            if (dp[node][0] != cost) continue;
            for (auto &i: g[node]) {
                for (edge j: i.second) {
                    // Case 1: We don't flip j
                    int case1 = psum[node][j.c] - j.p + cost;
                    if (!dp[j.to].count(0) || case1 < dp[j.to][0]) {
                        dp[j.to][0] = case1;
                        pq.push({-dp[j.to][0], j.to, 0});
                    }
                    // Case 2: We flip j but not another edge of the same colour
                    int case2 = j.p + cost;
                    if (!dp[j.to].count(0) || case2 < dp[j.to][0]) {
                        dp[j.to][0] = case2;
                        pq.push({-dp[j.to][0], j.to, 0});
                    }
                    // Case 3: We flip j and another edge of the same colour
                    int case3 = cost;
                    if (!dp[j.to].count(j.c) || case3 < dp[j.to][j.c]) {
                        dp[j.to][j.c] = case3;
                        pq.push({-dp[j.to][j.c], j.to, j.c});
                    }
                }
            }
        }
    }
    cout << (dp[n][0] >= inf ? -1 : dp[n][0]);
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...