Submission #601541

#TimeUsernameProblemLanguageResultExecution timeMemory
601541pakhomoveeRobot (JOI21_ho_t4)C++17
100 / 100
953 ms75416 KiB
#include <iostream> #include <vector> #include <algorithm> #include <set> #include <map> using namespace std; #define int long long const int maxn = 1e5; map<int, vector<pair<int, int>>> g[maxn]; map<int, int> dist[maxn]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); set<pair<pair<int, int>, int>> s; int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { int u, v, c, p; cin >> u >> v >> c >> p; --u; --v; g[u][c].push_back({ v, p }); g[v][c].push_back({ u, p }); } dist[0][0] = 0; s.insert({ { 0, 0 }, 0 }); auto upd = [&] (int u, int c, int d) { if (dist[u].count(c)) { if (dist[u][c] > d) { dist[u][c] = min(dist[u][c], d); s.insert({ { dist[u][c], u }, c }); } } else { dist[u][c] = d; s.insert({ { dist[u][c], u }, c }); } }; while (!s.empty()) { int v = s.begin()->first.second; int c = s.begin()->second; int d = s.begin()->first.first; s.erase(s.begin()); if (d > dist[v][c]) continue; if (c == 0) { for (auto [color, adjacent] : g[v]) { int sum = 0; for (auto [u, w] : adjacent) { sum += w; } for (auto [u, w] : adjacent) { upd(u, 0, d + min(w, sum - w)); upd(u, color, d); } } } else { int sum = 0; for (auto [u, w] : g[v][c]) { sum += w; } for (auto [u, w] : g[v][c]) { upd(u, 0, d + sum - w); } } } cout << (dist[n - 1].count(0) ? dist[n - 1][0] : -1); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...