結果
問題 | No.807 umg tours |
ユーザー |
![]() |
提出日時 | 2019-03-22 22:06:34 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 340 ms / 4,000 ms |
コード長 | 3,896 bytes |
コンパイル時間 | 1,314 ms |
コンパイル使用メモリ | 109,700 KB |
実行使用メモリ | 32,900 KB |
最終ジャッジ日時 | 2024-11-23 19:00:20 |
合計ジャッジ時間 | 6,451 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#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; }