Submission #41732


ソースコード

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
public class Main {
private static final InputStream in = System.in;
private static final PrintWriter out = new PrintWriter(System.out);
private static Vector<Pair>[] vii = new Vector[100100];
private static int[] cost = new int[100100];
public static void main(String[] args) {
// This is from IntelliJ IDEA
PriorityQueue<Pair> queue = new PriorityQueue<>(Collections.reverseOrder());
int n = fgetInt();
int m = fgetInt();
for ( int i = 1; i <= n; i++ ) {
cost[i] = 1 << 29;
vii[i] = new Vector<>();
}
for ( int i = 0; i < m; i++ ) {
int a = fgetInt();
int b = fgetInt();
int c = fgetInt();
vii[a].add(new Pair(b, c));
vii[b].add(new Pair(a, c));
}
queue.add(new Pair(0, 1));
while ( !queue.isEmpty() ) {
Pair now = queue.poll();
int cst = now.first;
int pos = now.second;
for ( int i = 0; i < vii[pos].size(); i++ ) {
int next = vii[pos].get(i).first;
int ncst = vii[pos].get(i).second;
if ( cst+ncst < cost[next] ) {
cost[next] = cst+ncst;
queue.add(new Pair(cst+ncst, next));
}
}
}
if ( cost[n] == 1 << 29 ) {
iprint("NA");
}
else {
iprint(cost[n]);
}
ifin();
}
static long gcd(long x, long y) {
return x % y == 0 ? y : gcd(y, x % y);
}
static long lcm(long x, long y) {
return ( x / gcd(x, y) * y);
}
static String fgetStr(){
return next();
}
static String fgetLine(){
return nextLine();
}
static char fgetChar(){
return fgetStr().charAt(0);
}
static int fgetInt(){
return nextInt();
}
static long fgetLong(){
return nextLong();
}
static double fgetDouble(){
return nextDouble();
}
static BigInteger fgetBig(){
return new BigInteger(fgetStr());
}
static String reverseStr(String str){
StringBuffer sb = new StringBuffer(str);
return sb.reverse().toString();
}
static void iprint(Object a){
out.println(a);
}
static void iprintf(Object a){
out.print(a);
}
static void ifin(){
out.flush();
}
private static final byte[] buffer = new byte[1024];
private static int ptr = 0;
private static int buflen = 0;
private static boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = in.read(buffer);
} catch ( IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private static int readByte() {
if (hasNextByte()) return buffer[ptr++];
else return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public static boolean hasNext() {
while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();
}
public static String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public static String nextLine() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b) || b == 32) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public static long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b)){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
public static int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public static double nextDouble() {
return Double.parseDouble(next());
}
}
class Pair implements Comparable<Pair>{
int first, second;
public Pair(int a, int b) {
this.first = a;
this.second = b;
}
@Override
public int compareTo( Pair pair ){
if ( pair.first == this.first ) {
return pair.second - this.second;
}
return pair.first - this.first;
}
}

ステータス

項目 データ
問題 0431 - 君も始めようダイクストラ大好き厨
ユーザー名 r1825
投稿日時 2018-08-24 07:00:47
言語 Java
状態 Accepted
得点 1
ソースコード長 5617 Byte
最大実行時間 207 ms
最大メモリ使用量 30056 KB

セット

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

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
m_in1.txt AC 207 ms 30056 KB
1
r_in1.txt AC 107 ms 16048 KB
1
r_in2.txt AC 107 ms 16152 KB
1
r_in3.txt AC 111 ms 16144 KB
1
r_in4.txt AC 97 ms 18492 KB
1
r_in5.txt AC 105 ms 18240 KB
1
r_in6.txt AC 114 ms 18444 KB
1
r_in7.txt AC 113 ms 15876 KB
1
r_in8.txt AC 101 ms 16284 KB
1
r_in9.txt AC 123 ms 20204 KB
1
r_in10.txt AC 93 ms 16304 KB
1
r_in11.txt AC 104 ms 18020 KB
1
r_in12.txt AC 125 ms 16104 KB
1
r_in13.txt AC 110 ms 16008 KB
1
r_in14.txt AC 119 ms 18328 KB
1
r_in15.txt AC 117 ms 18700 KB
1
r_in16.txt AC 94 ms 18112 KB
1
r_in17.txt AC 101 ms 18408 KB
1
r_in18.txt AC 102 ms 18356 KB
1
r_in19.txt AC 102 ms 16248 KB
1
r_in20.txt AC 111 ms 16152 KB
1
r_in21.txt AC 99 ms 15888 KB
1
r_in22.txt AC 97 ms 16060 KB
1
r_in23.txt AC 96 ms 16008 KB
1
r_in24.txt AC 112 ms 15960 KB
1
r_in25.txt AC 112 ms 15928 KB
1
r_in26.txt AC 114 ms 17896 KB
1
r_in27.txt AC 121 ms 16164 KB
1
r_in28.txt AC 107 ms 16004 KB
1
r_in29.txt AC 109 ms 16664 KB
1
r_in30.txt AC 110 ms 18060 KB
1