제출 #981615

#제출 시각아이디문제언어결과실행 시간메모리
9816150x34cRobot (JOI21_ho_t4)C++17
34 / 100
3028 ms82868 KiB
#include <bits/stdc++.h> #define ll long long #define pii pair<int, int> #define endl '\n' #define int ll using namespace std; const int INF = 2e15; struct nei { int v, c, w, e_idx; }; int N, M; vector<vector<nei>> graph; vector<map<int, int>> color_cost; vector<pii> conv; class Compare { public: bool operator()(tuple<int, int, int> &a, tuple<int, int, int> &b) { return get<0>(a) > get<0>(b); } }; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> M; graph.resize(N); color_cost.resize(N); conv.resize(M + 1, {0, 0}); for (int i = 1; i <= M; i++) { int a, b, c, w; cin >> a >> b >> c >> w; --a; --b; --c; graph[a].push_back({b, c, w, i}); graph[b].push_back({a, c, w, i}); conv[i] = {c, w}; } for (int i = 0; i < N; i++) { for (nei &u : graph[i]) color_cost[i][u.c] += u.w; } vector<map<int, int>> dist(N); // wei, node, prev color, prev color weight priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, Compare> q; dist[0][0] = 0; q.push({0, 0, 0}); while (!q.empty()) { int w, v, e_id; tie(w, v, e_id) = q.top(); q.pop(); if (dist[v][e_id] != w) continue; for (nei &u : graph[v]) { int opt1 = w + u.w; if (!dist[u.v].count(u.e_idx) || dist[u.v][u.e_idx] > opt1) { dist[u.v][u.e_idx] = opt1; q.push({opt1, u.v, u.e_idx}); } int pc, pw; tie(pc, pw) = conv[e_id]; int opt2 = w + color_cost[v][u.c] - u.w - (pc == u.c ? pw : 0); if (!dist[u.v].count(0) || dist[u.v][0] > opt2) { dist[u.v][0] = opt2; q.push({opt2, u.v, 0}); } } } int mn = INF; for (auto mp : dist[N - 1]) { mn = min(mn, mp.second); } cout << (mn == INF ? -1 : mn) << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...