-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ17396.java
More file actions
87 lines (76 loc) · 2.31 KB
/
BOJ17396.java
File metadata and controls
87 lines (76 loc) · 2.31 KB
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
package ¹éÁØ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class BOJ17396 {
static int N, M;
static int[] wad;
static boolean[] visited;
static long[] dist;
static ArrayList<ArrayList<Node>> list = new ArrayList<>();
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
wad = new int[N];
dist = new long[N];
visited = new boolean[N];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < N; i++) {
list.add(new ArrayList<>());
wad[i] = Integer.parseInt(st.nextToken());
}
for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int weight = Integer.parseInt(st.nextToken());
list.get(from).add(new Node(to, weight));
list.get(to).add(new Node(from, weight));
}
dijkstra(0);
if(dist[N-1] == Long.MAX_VALUE) {
System.out.print("-1");
}else {
System.out.print(dist[N-1]);
}
}
private static void dijkstra(int start) {
Arrays.fill(visited, false);
Arrays.fill(dist, Long.MAX_VALUE);
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.offer(new Node(start, 0));
dist[start] = 0;
while(!pq.isEmpty()) {
Node node = pq.poll();
int nodePoint = node.end;
if(visited[nodePoint]) continue;
visited[nodePoint] = true;
for(Node temp : list.get(nodePoint)) {
if((temp.end != (N-1)) && (wad[temp.end] == 1)) continue;
if(dist[nodePoint] + temp.weight < dist[temp.end]) {
dist[temp.end] = dist[nodePoint] + temp.weight;
pq.offer(new Node(temp.end, dist[temp.end]));
}
}
}
}
static class Node implements Comparable<Node>{
int end;
long weight;
Node(int end, long weight){
this.end = end;
this.weight = weight;
}
@Override
public int compareTo(Node o) {
if(this.weight - o.weight > 0) return 1;
else return -1;
}
}
}