結果
問題 | No.2739 Time is money |
ユーザー |
![]() |
提出日時 | 2024-04-21 01:03:04 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 366 ms / 2,000 ms |
コード長 | 1,775 bytes |
コンパイル時間 | 1,177 ms |
コンパイル使用メモリ | 102,952 KB |
実行使用メモリ | 21,148 KB |
最終ジャッジ日時 | 2024-10-12 23:23:34 |
合計ジャッジ時間 | 7,663 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
#include <concepts> #include <iostream> #include <limits> #include <queue> #include <vector> template <std::unsigned_integral T> struct WeightedGraph { constexpr static T INF = std::numeric_limits<T>::max() >> 1; using edge_type = std::pair<T, size_t>; private: std::vector<std::vector<edge_type>> graph; public: explicit WeightedGraph(size_t size = 0) : graph(size) {} void add_edge(size_t from, size_t to, T cost) { graph.at(from).emplace_back(cost, to); } void add_undirected_edge(size_t u, size_t v, T cost) { add_edge(u, v, cost), add_edge(v, u, cost); } std::vector<T> dijkstra(size_t start) const { std::vector<T> min_costs(graph.size(), INF); std::vector<bool> done(graph.size()); std::priority_queue<edge_type, std::vector<edge_type>, std::greater<>> pq; pq.emplace(0, start); while (pq.size()) { auto [cost, v] = pq.top(); pq.pop(); if (done.at(v)) continue; min_costs.at(v) = cost, done.at(v) = true; for (const auto &[edge_cost, to] : graph.at(v)) { T new_cost = cost + edge_cost; if (new_cost < min_costs.at(to)) pq.emplace(new_cost, to); } } return min_costs; } }; int main() { using namespace std; using ull = unsigned long long; ull n, m, x; cin >> n >> m >> x; WeightedGraph<ull> g(n + 1); for (ull u, v, c, t, i = 0; i < m; ++i) cin >> u >> v >> c >> t, g.add_undirected_edge(u, v, c + t * x); auto cost = g.dijkstra(1).at(n); if (cost == WeightedGraph<ull>::INF) cout << "-1\n"; else cout << (cost + x - 1) / x << '\n'; return 0; }