結果
問題 |
No.3111 Toll Optimization
|
ユーザー |
|
提出日時 | 2025-09-21 19:24:46 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 167 ms / 5,000 ms |
コード長 | 2,166 bytes |
コンパイル時間 | 3,416 ms |
コンパイル使用メモリ | 296,856 KB |
実行使用メモリ | 14,656 KB |
最終ジャッジ日時 | 2025-09-21 19:24:57 |
合計ジャッジ時間 | 9,816 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 70 |
ソースコード
#include <bits/stdc++.h> [[nodiscard]] static inline constexpr std::vector<std::vector<std::pair<uint_least32_t, uint_least32_t>>> prepare(const uint_fast32_t N, const uint_fast32_t M, const std::vector<uint_least32_t>& C, const std::vector<std::pair<uint_least32_t, uint_least32_t>>& bridges) noexcept { std::vector<std::vector<std::pair<uint_least32_t, uint_least32_t>>> next_of(N + 1); for (uint_fast32_t i = 0; i != M; ++i) next_of[bridges[i].first].emplace_back(bridges[i].second, C[i]), next_of[bridges[i].second].emplace_back(bridges[i].first, C[i]); return next_of; } [[nodiscard]] static inline int_fast64_t solve(const uint_fast32_t N, const uint_fast32_t K, const std::vector<std::vector<std::pair<uint_least32_t, uint_least32_t>>>& next_of) noexcept { std::vector<std::array<uint_least64_t, 4>> dist(N + 1, { UINT_LEAST64_MAX, UINT_LEAST64_MAX, UINT_LEAST64_MAX, UINT_LEAST64_MAX }); std::priority_queue<std::tuple<uint_least64_t, uint_least32_t, uint_least32_t>, std::vector<std::tuple<uint_least64_t, uint_least32_t, uint_least32_t>>, std::greater<std::tuple<uint_least64_t, uint_least32_t, uint_least32_t>>> pq; for (uint_fast32_t i = 0; i <= K; ++i) dist[1][i] = 0, pq.emplace(0, i, 1); while (!pq.empty()) { const auto [cur_dist, used_ticket, cur_pos] = pq.top(); pq.pop(); if (cur_dist != dist[cur_pos][used_ticket]) continue; for (const auto& [next_pos, cost] : next_of[cur_pos]) { if (dist[next_pos][used_ticket] > cur_dist + cost) dist[next_pos][used_ticket] = cur_dist + cost, pq.emplace(cur_dist + cost, used_ticket, next_pos); if (used_ticket < K && dist[next_pos][used_ticket + 1] > cur_dist) dist[next_pos][used_ticket + 1] = cur_dist, pq.emplace(cur_dist, used_ticket + 1, next_pos); } } return dist[N][K]; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); uint_fast32_t N, M, K; std::cin >> N >> M >> K; std::vector<uint_least32_t> C(M); std::vector<std::pair<uint_least32_t, uint_least32_t>> bridges(M); for (auto& c : C) std::cin >> c; for (auto& [u, v] : bridges) std::cin >> u >> v; std::cout << solve(N, K, prepare(N, M, C, bridges)) << '\n'; return 0; }