Submission #970877

#TimeUsernameProblemLanguageResultExecution timeMemory
970877htphong0909Robot (JOI21_ho_t4)C++17
34 / 100
3060 ms56032 KiB
#include <bits/stdc++.h> #define int long long using namespace std; vector<array<int, 3>> adj[100001]; int DP[100001]; unordered_map<int, int> DP2[100001], sum[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; adj[u].push_back({v, c, w}); adj[v].push_back({u, c, w}); sum[u][c] += w; sum[v][c] += w; } memset(DP, 0x3f, sizeof DP); DP[1] = 1; priority_queue<array<int, 3>, vector<array<int, 3>>, greater<>> q; q.push({DP[1], 1, 0}); while (!q.empty()) { auto [d, u, pc] = q.top(); q.pop(); for (const auto& [v, c, w] : adj[u]) { bool k = false; if (pc == c && DP[v] > DP2[u][c] + sum[u][c] - w) { DP[v] = DP2[u][c] + sum[u][c] - w; k = true; } if (DP[v] > DP[u] + min(w, sum[u][c] - w)) { DP[v] = DP[u] + min(w, sum[u][c] - w); k = true; } if (k) q.push({DP[v], v, 0}); if (DP2[v][c] > DP[u] || !DP2[v][c]) { DP2[v][c] = DP[u]; q.push({DP2[v][c], v, c}); } } } if (DP[n] > (int)1e18) cout << -1; else cout << DP[n] - 1; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...