Submission #388318

#TimeUsernameProblemLanguageResultExecution timeMemory
388318KephaRobot (JOI21_ho_t4)C++11
100 / 100
1284 ms79212 KiB
#include <bits/stdc++.h> #define INF 0x3f3f3f3f3f3f3f3f #define MX 100000 #define ll long long using namespace std; struct Edge { int color, next; ll price; Edge (int _color = 0, int _next = 0, ll _price = 0) { color = _color, next = _next, price = _price; } }; int n, m; map<int, vector<Edge>> adj[MX + 1]; ll dp[MX + 1]; map<int, ll> dp2[MX + 1], sum[MX + 1]; int main() {; cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v, c; ll p; cin >> u >> v >> c >> p; adj[u][c].push_back(Edge(c, v, p)); adj[v][c].push_back(Edge(c, u, p)); sum[u][c] += p; sum[v][c] += p; } fill(dp + 1, dp + 1 + n, INF); dp[1] = 0; priority_queue<tuple<ll, int, int>> pq; pq.push({0, 1, 0}); while (pq.size()) { ll cost; int node, c; tie(cost, node, c) = pq.top(); pq.pop(); if (c) { if (dp2[node][c] != -cost) continue; for (Edge i : adj[node][c]) { // We can't flip i in this case ll case1 = sum[node][c] - i.price; if (case1 - cost < dp[i.next]) { dp[i.next] = case1 - cost; pq.push({-dp[i.next], i.next, 0}); } } } else { if (dp[node] != -cost) continue; for (auto& i : adj[node]) { for (Edge j : i.second) { // Case 1: We don't flip j ll case1 = sum[node][j.color] - j.price - cost; if (case1 < dp[j.next]) { dp[j.next] = case1; pq.push({-dp[j.next], j.next, 0}); } // Case 2: We flip j but not another edge of the same colour ll case2 = j.price - cost; if (case2 < dp[j.next]) { dp[j.next] = case2; pq.push({-dp[j.next], j.next, 0}); } // Case 3: We flip j and another edge of the same colour ll case3 = -cost; if (!dp2[j.next].count(j.color) || case3 < dp2[j.next][j.color]) { dp2[j.next][j.color] = case3; pq.push({-dp2[j.next][j.color], j.next, j.color}); } } } } } 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...