Submission #58160
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | #include "bits/stdc++.h" // Begin Header {{{ #define all(x) (x).begin(), (x).end() #define rep(i, n) for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i) #define reps(i, s, t) for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i) #define repr(i, s, t) for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i) #define var(Type, ...) Type __VA_ARGS__; input(__VA_ARGS__) #ifndef DBG #define trace(...) #endif using namespace std; using i64 = int_fast64_t; using pii = pair<i64, i64>; template < class T, class U> inline bool chmax(T &a, const U &b) { return b > a && (a = b, true ); } template < class T, class U> inline bool chmin(T &a, const U &b) { return b < a && (a = b, true ); } inline i64 sigma(i64 n) { return (n * (n + 1) >> 1); } inline i64 updiv(i64 a, i64 b) { return (a + b - 1) / b; } inline i64 sqr(i64 n) { return n * n; } inline string to_string( char c) { return string(1, c); } constexpr int INF = 0x3f3f3f3f; constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL; template < class T> inline vector<T> make_v( const T &initValue, size_t sz) { return vector<T>(sz, initValue); } template < class T, class ... Args> inline auto make_v( const T &initValue, size_t sz, Args... args) { return vector<decltype(make_v<T>(initValue, args...))>(sz, make_v<T>(initValue, args...)); } inline void input() {} template < class Head, class ... Tail> inline void input(Head &head, Tail&... tail) { cin >> head; input(tail...); } inline void print() { cout << "\n" ; } template < class Head, class ... Tail> inline void print(Head &&head, Tail&&... tail) { cout << head; if ( sizeof ...(tail)) cout << ' ' ; print(forward<Tail>(tail)...); } template < class T> inline ostream& operator<< (ostream &out, const vector<T> &vec) { static constexpr const char *delim[] = { " " , "" }; for ( const auto &e : vec) out << e << delim[&e == &vec.back()]; return out; } template < typename Func> struct FixPoint : Func { inline constexpr FixPoint(Func &&f) noexcept : Func(forward<Func>(f)) {} template < typename ... Args> inline decltype(auto) operator()(Args &&... args) const { return Func::operator()(* this , forward<Args>(args)...); } }; template < typename Func > inline decltype(auto) makeFixPoint(Func &&f) { return FixPoint< Func >{forward< Func >(f)}; } // }}} End Header // graph {{{ struct Edge { // {{{ int src, to; int64_t cost = 0; inline Edge() noexcept {} inline Edge( int s, int t, int64_t c = 1) noexcept : src(s), to(t), cost(c) {} inline bool operator<( const Edge &rhs) const noexcept { return cost < rhs.cost; } inline bool operator>( const Edge &rhs) const noexcept { return rhs < * this ; } inline operator int () const noexcept { return to; } inline Edge& operator=( int rhs_to) noexcept { this ->to = rhs_to; return * this ; } }; // }}} class Graph : public vector<vector<Edge>> { // {{{ using super = vector<vector<Edge>>; public : Graph() noexcept {} Graph( int V) noexcept : super(V) {} Graph& addEdge( int from, int to, int64_t cost = 1) noexcept { super::operator[](from).emplace_back(from, to, cost); return * this ; } Graph& addEdge( const Edge &e) noexcept { return addEdge(e.src, e.to, e.cost); } }; // }}} // }}} template < class Compare = less<>, int64_t INF = 0x3f3f3f3f3f3f3f3fLL> vector<int64_t> dijkstra( const Graph &G, const vector< int > &startNodes) // {{{ { using pii = pair<int64_t, int >; const auto isBetter = Compare(); vector<int64_t> dist(G.size(), INF); priority_queue<pii, vector<pii>, Compare> pq; for ( const int startNode : startNodes) { dist[startNode] = 0LL; pq.emplace(-0LL, startNode); } while (!pq.empty()) { const int64_t nowCost = -(pq.top().first); const int nowNode = pq.top().second; pq.pop(); if (isBetter(dist[nowNode], nowCost)) continue ; for ( const auto &e : G[nowNode]) { const int64_t newCost = nowCost + e.cost; if (isBetter(newCost, dist[e.to])) { dist[e.to] = newCost; pq.emplace(-newCost, e.to); } } } return dist; } // }}} signed main() { ios::sync_with_stdio( false ); cin.tie(nullptr); var( int , V, E); Graph G(V); rep(i, E) { var( int , a, b, c); --a, --b; G.addEdge(a, b, c); G.addEdge(b, a, c); } const auto dist = dijkstra(G, {0}); print(dist[V-1]); return 0; } |
ステータス
項目 | データ |
---|---|
問題 | 1257 - ダイクストラ大好き厨とコーナーテストケース |
ユーザー名 | Arumakan_ei1727 |
投稿日時 | 2020-01-15 21:40:05 |
言語 | C++14 |
状態 | Accepted |
得点 | 5 |
ソースコード長 | 4692 Byte |
最大実行時間 | 107 ms |
最大メモリ使用量 | 14664 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 5 / 5 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
corner_input.txt | AC | 63 ms | 13516 KB |
1
|
input01.txt | AC | 107 ms | 14664 KB |
1
|
input02.txt | AC | 60 ms | 9272 KB |
1
|
input03.txt | AC | 64 ms | 8480 KB |
1
|
input04.txt | AC | 78 ms | 11384 KB |
1
|
input05.txt | AC | 61 ms | 11224 KB |
1
|
sample_input.txt | AC | 16 ms | 596 KB |
1
|