結果

問題 No.2739 Time is money
ユーザー true-moleculetrue-molecule
提出日時 2024-04-21 01:03:04
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 516 ms / 2,000 ms
コード長 1,775 bytes
コンパイル時間 1,338 ms
コンパイル使用メモリ 103,620 KB
実行使用メモリ 21,144 KB
最終ジャッジ日時 2024-04-21 01:03:14
合計ジャッジ時間 9,858 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 209 ms
13,568 KB
testcase_03 AC 405 ms
17,680 KB
testcase_04 AC 196 ms
13,056 KB
testcase_05 AC 288 ms
13,728 KB
testcase_06 AC 376 ms
16,976 KB
testcase_07 AC 489 ms
19,984 KB
testcase_08 AC 508 ms
20,224 KB
testcase_09 AC 477 ms
20,480 KB
testcase_10 AC 378 ms
19,756 KB
testcase_11 AC 516 ms
20,480 KB
testcase_12 AC 372 ms
18,816 KB
testcase_13 AC 376 ms
18,816 KB
testcase_14 AC 343 ms
18,820 KB
testcase_15 AC 250 ms
17,908 KB
testcase_16 AC 247 ms
17,068 KB
testcase_17 AC 475 ms
21,136 KB
testcase_18 AC 465 ms
21,144 KB
testcase_19 AC 327 ms
17,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0