-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ1504.java
More file actions
93 lines (82 loc) · 2.44 KB
/
BOJ1504.java
File metadata and controls
93 lines (82 loc) · 2.44 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
88
89
90
91
92
93
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 BOJ1504 {
static int N, M;
static boolean[] visited;
static int[] dist;
static final int INF = 200_000_000;
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());
visited = new boolean[N+1];
dist = new int[N+1];
for(int i = 0; i <= N; i++) {
list.add(new ArrayList<>());
}
for(int i = 1; i <= M; i++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int len = Integer.parseInt(st.nextToken());
list.get(from).add(new Node(to, len));
list.get(to).add(new Node(from, len));
}
st = new StringTokenizer(br.readLine());
int node1 = Integer.parseInt(st.nextToken());
int node2 = Integer.parseInt(st.nextToken());
int ans1 = 0;
int ans2 = 0;
ans1 += dijkstra(1, node1);
ans1 += dijkstra(node1, node2);
ans1 += dijkstra(node2, N);
ans2 += dijkstra(1, node2);
ans2 += dijkstra(node2, node1);
ans2 += dijkstra(node1, N);
if(Math.min(ans1, ans2) >= INF) {
System.out.print("-1");
return;
}
System.out.print(Math.min(ans1, ans2));
}
public static int dijkstra(int start, int end) {
Arrays.fill(visited, false);
Arrays.fill(dist, INF);
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.offer(new Node(start, 0));
dist[start] = 0;
while(!pq.isEmpty()) {
Node temp = pq.poll();
int no = temp.no;
if(visited[no]) continue;
visited[no] = true;
for(Node adj : list.get(no)) {
if(dist[adj.no] > dist[no] + adj.length) {
dist[adj.no] = dist[no] + adj.length;
pq.offer(new Node(adj.no, dist[adj.no]));
}
}
}
return dist[end];
}
public static class Node implements Comparable<Node>{
int no;
int length;
Node(int no, int length){
this.no = no;
this.length = length;
}
@Override
public int compareTo(Node o) {
return this.length - o.length;
}
}
}