|
| 1 | +package week9.군사이동; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.PriorityQueue; |
| 8 | +import java.util.StringTokenizer; |
| 9 | + |
| 10 | +public class BOJ_11085 { |
| 11 | + static int[] parent; |
| 12 | + static int p, w, c, v; |
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 15 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 16 | + |
| 17 | + p = Integer.parseInt(st.nextToken()); |
| 18 | + w = Integer.parseInt(st.nextToken()); |
| 19 | + |
| 20 | + st = new StringTokenizer(br.readLine()); |
| 21 | + c = Integer.parseInt(st.nextToken()); |
| 22 | + v = Integer.parseInt(st.nextToken()); |
| 23 | + |
| 24 | + parent = new int[p+1]; |
| 25 | + for(int i=1; i<=p; i++){ |
| 26 | + parent[i] = i; |
| 27 | + } |
| 28 | + PriorityQueue<int[]> priorityQueue = new PriorityQueue<>(((o1, o2) -> o2[2]-o1[2])); |
| 29 | + for(int i=0; i<w; i++){ |
| 30 | + priorityQueue.add(Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray()); |
| 31 | + } |
| 32 | + |
| 33 | + int result = 0; |
| 34 | + while(!priorityQueue.isEmpty()){ |
| 35 | + int[] now = priorityQueue.poll(); |
| 36 | + int start = now[0]; |
| 37 | + int end = now[1]; |
| 38 | + int width = now[2]; |
| 39 | + |
| 40 | + union(start, end); |
| 41 | + |
| 42 | + if(find(c)==find(v)){ |
| 43 | + result = width; |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + System.out.println(result); |
| 49 | + } |
| 50 | + |
| 51 | + static int find(int x){ |
| 52 | + if(x==parent[x])return x; |
| 53 | + return parent[x]=find(parent[x]); |
| 54 | + } |
| 55 | + |
| 56 | + static void union(int x, int y){ |
| 57 | + x = find(x); |
| 58 | + y = find(y); |
| 59 | + |
| 60 | + if(x==y)return; |
| 61 | + |
| 62 | + parent[y] = x; |
| 63 | + } |
| 64 | +} |
0 commit comments