Submission #58680


ソースコード

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <cstring>
#include <iomanip>
#include <utility>
#include <math.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,n) for(int i=1;i<=(n);i++)
#define lol long long
#define mp make_pair
#define fi first
#define se second
#define pu push_back
#define SYOU(x) setprecision(x+1)
#define abs(x,y) (max(x,y)-min(x,y))
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD=int(1e9)+7;
using namespace std;
using pii = pair<int,int>;
vector<vector<pii> > graph;
vector<int> min_cost;
void Dijkstra(int s){
priority_queue<pii, vector<pii>, greater<pii> > pq;
min_cost[s] = 0;
pq.emplace(0, s);
while(!pq.empty()){
int now_cost, now_node;
tie(now_cost, now_node) = pq.top();
pq.pop();
if(min_cost[now_node] < now_cost){
continue;
}
for(int i = 0; i < graph[now_node].size(); ++i){
int next_node, next_cost;
tie(next_node, next_cost) = graph[now_node][i];
if(min_cost[now_node] + next_cost < min_cost[next_node]){
min_cost[next_node] = min_cost[now_node] + next_cost;
pq.emplace(min_cost[next_node], next_node);
}
}
}
}
signed main(void){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int N, M;
cin >> N >> M;
min_cost.resize(N+1, INF);
graph.resize(N+1);
for(int i = 0; i < M; ++i){
int a, b, c;
cin >> a >> b >> c;
graph[a].emplace_back(b, c);
graph[b].emplace_back(a, c);
}
Dijkstra(1);
if(min_cost[N] == INF){
cout << "NA\n";
} else {
cout << min_cost[N] << '\n';
}
return 0;
}

ステータス

項目 データ
問題 0431 - 君も始めようダイクストラ大好き厨
ユーザー名 NASSUN_ei1906
投稿日時 2020-02-14 16:58:59
言語 C++14
状態 Accepted
得点 1
ソースコード長 1851 Byte
最大実行時間 63 ms
最大メモリ使用量 7004 KB

セット

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

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
m_in1.txt AC 63 ms 7004 KB
1
r_in1.txt AC 35 ms 1432 KB
1
r_in2.txt AC 30 ms 1652 KB
1
r_in3.txt AC 26 ms 1780 KB
1
r_in4.txt AC 25 ms 1928 KB
1
r_in5.txt AC 38 ms 1912 KB
1
r_in6.txt AC 27 ms 2100 KB
1
r_in7.txt AC 23 ms 1360 KB
1
r_in8.txt AC 25 ms 1316 KB
1
r_in9.txt AC 35 ms 2480 KB
1
r_in10.txt AC 21 ms 1248 KB
1
r_in11.txt AC 30 ms 1604 KB
1
r_in12.txt AC 32 ms 1688 KB
1
r_in13.txt AC 25 ms 1708 KB
1
r_in14.txt AC 28 ms 1756 KB
1
r_in15.txt AC 30 ms 2000 KB
1
r_in16.txt AC 18 ms 1940 KB
1
r_in17.txt AC 22 ms 2052 KB
1
r_in18.txt AC 29 ms 2088 KB
1
r_in19.txt AC 19 ms 904 KB
1
r_in20.txt AC 23 ms 1140 KB
1
r_in21.txt AC 20 ms 916 KB
1
r_in22.txt AC 24 ms 868 KB
1
r_in23.txt AC 26 ms 976 KB
1
r_in24.txt AC 20 ms 1108 KB
1
r_in25.txt AC 31 ms 1184 KB
1
r_in26.txt AC 30 ms 1264 KB
1
r_in27.txt AC 29 ms 1324 KB
1
r_in28.txt AC 30 ms 1340 KB
1
r_in29.txt AC 35 ms 1540 KB
1
r_in30.txt AC 26 ms 1608 KB
1