結果
問題 | No.807 umg tours |
ユーザー | fooractal |
提出日時 | 2019-03-22 22:06:34 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 436 ms / 4,000 ms |
コード長 | 3,896 bytes |
コンパイル時間 | 1,389 ms |
コンパイル使用メモリ | 109,608 KB |
実行使用メモリ | 32,876 KB |
最終ジャッジ日時 | 2024-05-02 23:23:41 |
合計ジャッジ時間 | 6,882 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 3 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 3 ms
5,376 KB |
testcase_05 | AC | 3 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 199 ms
24,124 KB |
testcase_12 | AC | 233 ms
19,172 KB |
testcase_13 | AC | 299 ms
25,116 KB |
testcase_14 | AC | 123 ms
12,824 KB |
testcase_15 | AC | 99 ms
10,520 KB |
testcase_16 | AC | 316 ms
26,104 KB |
testcase_17 | AC | 421 ms
31,880 KB |
testcase_18 | AC | 433 ms
31,648 KB |
testcase_19 | AC | 406 ms
29,232 KB |
testcase_20 | AC | 250 ms
16,008 KB |
testcase_21 | AC | 254 ms
16,356 KB |
testcase_22 | AC | 102 ms
8,852 KB |
testcase_23 | AC | 78 ms
7,772 KB |
testcase_24 | AC | 224 ms
22,424 KB |
testcase_25 | AC | 436 ms
32,876 KB |
ソースコード
#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; }