結果

問題 No.807 umg tours
ユーザー fooractalfooractal
提出日時 2019-03-22 22:06:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 406 ms / 4,000 ms
コード長 3,896 bytes
コンパイル時間 1,068 ms
コンパイル使用メモリ 108,736 KB
実行使用メモリ 33,988 KB
最終ジャッジ日時 2023-08-15 11:58:17
合計ジャッジ時間 6,722 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 183 ms
25,556 KB
testcase_12 AC 219 ms
19,100 KB
testcase_13 AC 292 ms
26,376 KB
testcase_14 AC 122 ms
12,884 KB
testcase_15 AC 92 ms
10,544 KB
testcase_16 AC 296 ms
26,284 KB
testcase_17 AC 406 ms
33,100 KB
testcase_18 AC 398 ms
32,788 KB
testcase_19 AC 383 ms
29,096 KB
testcase_20 AC 238 ms
15,860 KB
testcase_21 AC 241 ms
16,188 KB
testcase_22 AC 95 ms
8,864 KB
testcase_23 AC 73 ms
7,544 KB
testcase_24 AC 219 ms
23,684 KB
testcase_25 AC 405 ms
33,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <numeric>
#include <string>
#include <sstream>
#include <cmath>
#include <cstring>
#include <assert.h>
#include <utility>
#include <tuple>
#include <array>
#include <bitset>
#include <cstdlib>

using int64 = long long;

template <typename T = int>
struct Range {
    struct RangeIterator {
        T i, step;

        RangeIterator(T i, T step): i{i}, step{step} {}

        T& operator*() { return i; }
        RangeIterator& operator++() {
            i += step;
            return *this;
        }
        bool operator!=(const RangeIterator& rhs) const {
            return (step > 0 && i < rhs.i) || (step < 0 && i > rhs.i);
        }
    };

    T start, stop, step;

    Range(T start, T stop, T step): start{start}, stop{stop}, step{step} {}
    Range(T start, T stop): Range(start, stop, 1) {}
    Range(T stop): Range(0, stop, 1) {}
    
    RangeIterator begin() const { return RangeIterator(start, step); }
    RangeIterator end() const { return RangeIterator(stop, step); }
};

struct Edge {
    using Weight = int64;

    int end1_, end2_, from_, to_;
    Weight weight_;

    Edge() = default;
    Edge(int end1, int end2, Weight w):
        end1_{end1}, end2_{end2}, from_{end1}, to_{end2}, weight_{w} {}
    
    bool operator<(const Edge& e) const {
        return weight_ < e.weight_;
    }
};

struct UndirectedGraph {
    int num_nodes_;
    std::vector<std::vector<Edge>> adjacent_list_;
    std::vector<Edge> edges_;

    UndirectedGraph() = default;
    UndirectedGraph(int num_node): num_nodes_{num_node}, adjacent_list_(num_node) {}

    const std::vector<Edge>& operator[](int u) const { return adjacent_list_[u]; }
    std::vector<Edge>& operator[](int u) { return adjacent_list_[u]; }

    void addEdge(int end1, int end2, Edge::Weight weight) {
        adjacent_list_[end1].emplace_back(end1, end2, weight);
        adjacent_list_[end2].emplace_back(end2, end1, weight);
        edges_.emplace_back(end1, end2, weight);
    }
};

int64 cost[100000][2];

void dijkstra(const UndirectedGraph& graph, int start) {
    struct State {
        int node;
        int use_ticket;
        int64 cost;

        State(int node, int use_ticket, int64 cost): node{node}, use_ticket{use_ticket}, cost{cost} {}
        bool operator>(const State& rhs) const {
            return cost > rhs.cost;
        }
    };

    constexpr int64 INF = (1LL << 50);
    for (int u = 0; u < graph.num_nodes_; u++) {
        cost[u][0] = cost[u][1] = INF;
    }

    std::priority_queue<State, std::vector<State>, std::greater<State>> pque;
    cost[start][0] = cost[start][1] = 0;
    pque.emplace(start, 0, 0);

    while (!pque.empty()) {
        State st = pque.top(); pque.pop();

        if (cost[st.node][st.use_ticket] < st.cost) continue;

        for (const Edge& e : graph[st.node]) {
            for (int use_ticket : {0, 1}) {
                if (st.use_ticket && use_ticket) continue;

                int64 ncost = st.cost + (use_ticket ? 0 : e.weight_);

                if (cost[e.to_][st.use_ticket | use_ticket] > ncost) {
                    cost[e.to_][st.use_ticket | use_ticket] = ncost;
                    pque.emplace(e.to_, st.use_ticket | use_ticket, ncost);
                }
            }
        }
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    int n, m;
    std::cin >> n >> m;

    UndirectedGraph graph(n);
    for (const int i : Range<>(m)) {
        int a, b, c;
        std::cin >> a >> b >> c;
        a--; b--;
        graph.addEdge(a, b, c);
    }

    dijkstra(graph, 0);

    for (const int i : Range<>(n)) {
        std::cout << cost[i][0] + cost[i][1] << std::endl;
    }

    return 0;
}
0