Submission #971147

#TimeUsernameProblemLanguageResultExecution timeMemory
971147htphong0909Robot (JOI21_ho_t4)C++17
34 / 100
3036 ms74572 KiB
#include <bits/stdc++.h> #define int long long using namespace std; vector<array<int, 4>> adj[100001]; vector<vector<pair<int, int>>> adj2[100001]; int DP[100001]; int sum[200001]; vector<int> DP2[100001]; int cnt[200001]; map<int, int> hm[100001]; 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]); for (const auto& [v, c, w, vc] : adj[i]) adj2[i][c].emplace_back(v, 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(); for (int i = 0; i < cnt[u]; i++) sum[i] = 0; for (const array<int, 4>& cur : adj[u]) sum[cur[1]] += cur[2]; if (DP[u] == d && pc == -1) { for (const auto& [v, c, w, vc] : adj[u]) { int s = sum[c]; 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) { int s = sum[pc]; for (const auto& [v, w] : adj2[u][pc]) { if (DP[v] > DP2[u][pc] + s - w) { DP[v] = DP2[u][pc] + s - w; q.push({DP[v], v, -1}); } } } for (int i = 0; i < cnt[u]; i++) sum[i] = 0; } 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...