제출 #770591

#제출 시각아이디문제언어결과실행 시간메모리
770591BlagojRobot (JOI21_ho_t4)C++17
100 / 100
696 ms99264 KiB
#include <bits/stdc++.h> #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #endif #pragma GCC optimize("Ofast,unroll-loops") using namespace std; #define endl '\n' #define ll long long #define all(x) x.begin(), x.end() struct Node { ll to, color, cost; }; unordered_map<int, vector<Node>> edg[100005]; unordered_map<int, ll> sum[100005], dp2[100005]; ll dp[100005]; int n, m; struct T { ll dist, place, color; }; bool operator<(T a, T b) { return a.dist > b.dist; } void solve() { priority_queue<T> pq; pq.push({0, 1, 0}); while (pq.size() > 0) { T top = pq.top(); pq.pop(); if (top.color) { if (top.dist != dp2[top.place][top.color]) continue; for (auto [to, color, cost] : edg[top.place][top.color]) { ll newCost = top.dist + sum[top.place][color] - cost; if (newCost < dp[to]) { dp[to] = newCost; pq.push({dp[to], to, 0}); } } continue; } if (top.dist != dp[top.place]) continue; for (auto x : edg[top.place]) { for (auto [to, color, cost] : x.second) { ll newCost = top.dist + min(cost, sum[top.place][color] - cost); if (newCost < dp[to]) { dp[to] = newCost; pq.push({dp[to], to, 0}); } if (!dp2[to].count(color) || top.dist < dp2[to][color]) { dp2[to][color] = top.dist; pq.push({dp2[to][color], to, color}); } } } } cout << ((dp[n] == LLONG_MAX) ? -1 : dp[n]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 2; i <= n; i++) dp[i] = LLONG_MAX; for (int i = 0; i < m; i++) { ll from, to, color, cost; cin >> from >> to >> color >> cost; edg[from][color].push_back({to, color, cost}); edg[to][color].push_back({from, color, cost}); sum[from][color] += cost; sum[to][color] += cost; } solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...