# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
863244 | phi | Robot (JOI21_ho_t4) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <array>
#include <utility>
#include <queue>
#include <algorithm>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<vector<array<int, 3>>> adj(n);
for (int i = 0; i < m; i++) {
int a, b, c, p;
cin >> a >> b >> c >> p;
a--; b--;
adj[a].push_back({ c, p, b });
adj[b].push_back({ c, p, a });
}
vector<vector<long long>> dp(n);
vector<vector<array<int, 2>>> total(n);
for (int i = 0; i < n; i++) {
sort(adj[i].begin(), adj[i].end());
for (int j = 0; j < adj[i].size(); j++) {
if (j == 0 || total[i][total[i].size() - 1][0] != adj[i][j][0])
total[i].push_back({ adj[i][j][0], 0 });
total[i][total[i].size() - 1][1] += adj[i][j][1];
}
dp[i].resize(adj[i].size() + 1, 1e18);
}
priority_queue<pair<long long, array<int, 5>>> q;
q.push({ 0, { 0, 0, 0, 0, 0 } });
while (!q.empty()) {
auto [ d, x ] = q.top();
// from v to u
auto [ t, c, p, u, v ] = x;
q.pop();
assert(u < n);
if (dp[u][dp[u].size() - 1] > d) {
dp[u][dp[u].size() - 1] = d;
for (auto arr : adj[u]) {
int tot = (*lower_bound(total[u].begin(), total[u].end(), array<int, 2>{ arr[0], -1 }))[1];
q.push({ d + tot - arr[1], { 0, 0, 0, arr[2], u } });
q.push({ d + arr[1], { 1, arr[0], arr[1], arr[2], u } });
}
}
if (t == 0) {
auto st = lower_bound(adj[u].begin(), adj[u].end(), array<int, 3>{ c, p, v });
int i = st - adj[u].begin();
if (dp[u][i] <= d) continue;
dp[u][i] = d;
auto it = lower_bound(adj[u].begin(), adj[u].end(), array<int, 3>{ c + 1, -1, 0 });
it--;
if (it == st) it--;
if (it == adj[u].begin() || (*it)[0] != c) continue;
int tot = (*lower_bound(total[u].begin(), total[u].end(), array<int, 2>{ c, -1 }))[1];
auto [ c2, p2, v2 ] = *it;
q.push({ d + tot - p - p2, { 1, c2, p2, v2, u } });
}
}
long long ans = 1e18;
for (auto e : dp[n - 1])
ans = min(ans, e);
if (ans == 1e18) cout << -1;
else cout << ans;
return 0;
}