Submission #69290


ソースコード

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
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
struct edge{long long to, cost;};
typedef pair<long long, long long> P;
struct graph{
long long V;
vector<vector<edge> > G;
vector<long long> d;
graph(long long n){
V = n;
G.resize(V);
d.resize(V);
fill(d.begin(), d.end(), INF);
}
void add_edge(long long s, long long t, long long cost){
edge e1;
e1.to = t, e1.cost = cost;
G[s].push_back(e1);
//無向グラフのとき
edge e2;
e2.to = s, e2.cost = cost;
G[t].push_back(e2);
}
void dijkstra(long long s){
fill(d.begin(), d.end(), INF);
d[s] = 0;
priority_queue<P,vector<P>, greater<P> > pq;
pq.push(P(0,s));
while(!pq.empty()){
P p = pq.top(); pq.pop();
long long v = p.second;
if(d[v]<p.first) continue;
for(auto e : G[v]){
if(d[e.to]>d[v]+e.cost){
d[e.to] = d[v]+e.cost;
pq.push(P(d[e.to],e.to));
}
}
}
}
long long dist(long long t) {return d[t];}
};
int main(){
int n,m,i,a,b,c;
cin>>n>>m;
graph g(n);
for(i=0;i<m;i++){
cin>>a>>b>>c;
g.add_edge(a-1,b-1,c);
}
g.dijkstra(0);
long long z=g.dist(n-1);
if(z==INF){
cout<<"NA"<<endl;
}else{
cout<<z<<endl;
}
return(0);
}

ステータス

項目 データ
問題 0431 - 君も始めようダイクストラ大好き厨
ユーザー名 c2136
投稿日時 2022-02-14 17:36:55
言語 C++17
状態 Accepted
得点 1
ソースコード長 1409 Byte
最大実行時間 106 ms
最大メモリ使用量 10200 KB

セット

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

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
m_in1.txt AC 106 ms 10200 KB
1
r_in1.txt AC 36 ms 2156 KB
1
r_in2.txt AC 41 ms 2432 KB
1
r_in3.txt AC 37 ms 2336 KB
1
r_in4.txt AC 28 ms 2152 KB
1
r_in5.txt AC 47 ms 2928 KB
1
r_in6.txt AC 39 ms 2836 KB
1
r_in7.txt AC 33 ms 1620 KB
1
r_in8.txt AC 30 ms 1712 KB
1
r_in9.txt AC 58 ms 3228 KB
1
r_in10.txt AC 23 ms 1300 KB
1
r_in11.txt AC 38 ms 2060 KB
1
r_in12.txt AC 47 ms 2104 KB
1
r_in13.txt AC 39 ms 2244 KB
1
r_in14.txt AC 41 ms 2432 KB
1
r_in15.txt AC 36 ms 2508 KB
1
r_in16.txt AC 29 ms 2116 KB
1
r_in17.txt AC 27 ms 2268 KB
1
r_in18.txt AC 38 ms 2192 KB
1
r_in19.txt AC 38 ms 1252 KB
1
r_in20.txt AC 28 ms 1472 KB
1
r_in21.txt AC 31 ms 928 KB
1
r_in22.txt AC 30 ms 1208 KB
1
r_in23.txt AC 27 ms 1332 KB
1
r_in24.txt AC 30 ms 1448 KB
1
r_in25.txt AC 35 ms 1708 KB
1
r_in26.txt AC 33 ms 1888 KB
1
r_in27.txt AC 37 ms 2024 KB
1
r_in28.txt AC 28 ms 2068 KB
1
r_in29.txt AC 41 ms 1996 KB
1
r_in30.txt AC 37 ms 2196 KB
1