Submission #56944


ソースコード

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
#include "bits/stdc++.h"
#pragma GCC optimize("Ofast")
// Begin {{{
using namespace std;
#ifndef DEBUG
#define dump(...)
#endif
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
template <class T>
inline vector<T> make_v(const T &initvalue, int64_t sz) {
return vector<T>(sz, initvalue);
}
template <class T, class... Args>
inline auto make_v(const T &initvalue, int64_t sz, Args... args) {
return vector<decltype(make_v<T>(initvalue, args...))>(sz, make_v<T>(initvalue, args...));
}
constexpr int INF = 0x3f3f3f3f;
constexpr int64_t LINF = 0x3f3f3f3f3f3f3f3fLL;
// }}} End
using pii = pair<int64_t, int64_t>;
vector<pii> G[int(1e5)]; // {node, cost}
int64_t N, M;
vector<int64_t> cost(int(1e5), INF);
int64_t dijkstra() {
priority_queue<pii, vector<pii>, greater<>> que; // {cost, node}
cost[0] = 0;
que.emplace(0, 0);
while (!que.empty()) {
int64_t nowcost = que.top().first;
int64_t nownode = que.top().second;
que.pop();
if (cost[nownode] < nowcost) continue;
for (auto next: G[nownode]) {
if (chmin(cost[next.first], cost[nownode] + next.second)) {
que.emplace(cost[next.first], next.first);
}
}
}
return cost[N - 1];
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M;
for (int64_t i = 0; i < M; ++i) {
int64_t a, b, c;
cin >> a >> b >> c;
a--, b--;
G[a].emplace_back(b, c);
G[b].emplace_back(a, c);
}
int64_t res = dijkstra();
if (res == INF) {
cout << "NA" << "\n";
} else {
cout << res << "\n";
}
return 0;
}

ステータス

項目 データ
問題 0431 - 君も始めようダイクストラ大好き厨
ユーザー名 もけ
投稿日時 2019-12-06 16:08:27
言語 C++14
状態 Accepted
得点 1
ソースコード長 1911 Byte
最大実行時間 65 ms
最大メモリ使用量 10328 KB

セット

セット 得点 Cases
1 ALL 1 / 1 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
m_in1.txt AC 65 ms 10328 KB
1
r_in1.txt AC 29 ms 5324 KB
1
r_in2.txt AC 31 ms 5288 KB
1
r_in3.txt AC 29 ms 5488 KB
1
r_in4.txt AC 26 ms 4296 KB
1
r_in5.txt AC 30 ms 5688 KB
1
r_in6.txt AC 32 ms 5348 KB
1
r_in7.txt AC 28 ms 4532 KB
1
r_in8.txt AC 30 ms 4556 KB
1
r_in9.txt AC 35 ms 5656 KB
1
r_in10.txt AC 22 ms 4080 KB
1
r_in11.txt AC 30 ms 5032 KB
1
r_in12.txt AC 27 ms 5128 KB
1
r_in13.txt AC 33 ms 5140 KB
1
r_in14.txt AC 28 ms 5332 KB
1
r_in15.txt AC 35 ms 5196 KB
1
r_in16.txt AC 25 ms 4224 KB
1
r_in17.txt AC 27 ms 4356 KB
1
r_in18.txt AC 30 ms 4420 KB
1
r_in19.txt AC 27 ms 4288 KB
1
r_in20.txt AC 31 ms 4752 KB
1
r_in21.txt AC 26 ms 4028 KB
1
r_in22.txt AC 25 ms 4128 KB
1
r_in23.txt AC 23 ms 4356 KB
1
r_in24.txt AC 25 ms 4492 KB
1
r_in25.txt AC 28 ms 4644 KB
1
r_in26.txt AC 30 ms 4788 KB
1
r_in27.txt AC 27 ms 4924 KB
1
r_in28.txt AC 33 ms 4968 KB
1
r_in29.txt AC 32 ms 5076 KB
1
r_in30.txt AC 36 ms 4976 KB
1