제출 #971174

#제출 시각아이디문제언어결과실행 시간메모리
971174htphong0909Robot (JOI21_ho_t4)C++17
100 / 100
506 ms101288 KiB
#include <bits/stdc++.h> #define int long long using namespace std; vector<array<int, 4>> adj[200001]; vector<vector<array<int, 3>>> adj2[200001]; vector<int> sum[200001]; int DP[200001]; vector<int> DP2[200001]; int cnt[200001]; map<int, int> hm[200001]; int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); //freopen("TEST.IN", "r", stdin); //freopen("TEST.OUT", "w", stdout); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v, c, w; cin >> u >> v >> c >> w; if (!hm[u].count(c)) hm[u][c] = cnt[u]++; if (!hm[v].count(c)) hm[v][c] = cnt[v]++; adj[u].push_back({v, hm[u][c], w, hm[v][c]}); adj[v].push_back({u, hm[v][c], w, hm[u][c]}); } for (int i = 1; i <= n; i++) { DP2[i].assign(cnt[i], (int)1e18); adj2[i].resize(cnt[i]); sum[i].assign(cnt[i], 0); for (const auto& [v, c, w, vc] : adj[i]) { adj2[i][c].push_back({v, w, vc}); sum[i][c] += w; } } memset(DP, 0x3f, sizeof DP); DP[1] = 0; priority_queue<array<int, 3>, vector<array<int, 3>>, greater<>> q; q.push({DP[1], 1, -1}); while (!q.empty()) { auto [d, u, pc] = q.top(); q.pop(); if (DP[u] == d && pc == -1) { for (int i = 0; i < cnt[u]; i++) { int s = sum[u][i]; for (const auto& [v, w, vc] : adj2[u][i]) { if (DP[v] > DP[u] + min(w, s - w)) { DP[v] = DP[u] + min(w, s - w); q.push({DP[v], v, -1}); } if (DP2[v][vc] > DP[u]) { DP2[v][vc] = DP[u]; q.push({DP2[v][vc], v, vc}); } } } } else if (DP2[u][pc] == d && pc != -1) { int s = sum[u][pc]; for (const auto& [v, w, vc] : adj2[u][pc]) { if (DP[v] > DP2[u][pc] + s - w) { DP[v] = DP2[u][pc] + s - w; q.push({DP[v], v, -1}); } } } } if (DP[n] > (int)1e18) cout << -1; else cout << DP[n]; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...