|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class SB_1939 { |
| 7 | + static int N,M, start, end; |
| 8 | + static List<List<Node>> adj = new ArrayList<>(); |
| 9 | + static int[] dist; |
| 10 | + |
| 11 | + private static int dijskra() { |
| 12 | + PriorityQueue<Node> pq = new PriorityQueue<>(); |
| 13 | + pq.offer(new Node(start, Integer.MAX_VALUE)); // 최소값(최소값 중 최대)을 찾는것이기 때문에 맥스값으로 시작 |
| 14 | + dist[start] = Integer.MAX_VALUE; |
| 15 | + |
| 16 | + while (!pq.isEmpty()) { |
| 17 | + Node cur = pq.poll(); |
| 18 | + if (cur.idx==end) return cur.c; // 도착점 도달 시 최대 중량 반환 |
| 19 | + |
| 20 | + if (cur.c < dist[cur.idx]) continue; |
| 21 | + for (Node nxt : adj.get(cur.idx)) { |
| 22 | + int weight = Math.min(cur.c, nxt.c); // 현재까지 중량과 다음 간선의 중량 중 최소값 |
| 23 | + if (dist[nxt.idx] < weight) { |
| 24 | + dist[nxt.idx] = weight; |
| 25 | + pq.offer(new Node(nxt.idx, weight)); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + return 0; // 도달할 수 없는 경우 |
| 30 | + } |
| 31 | + |
| 32 | + public static void main(String[] args) throws IOException { |
| 33 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 34 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 35 | + |
| 36 | + N = Integer.parseInt(st.nextToken()); |
| 37 | + M = Integer.parseInt(st.nextToken()); |
| 38 | + |
| 39 | + dist = new int[N+1]; |
| 40 | + Arrays.fill(dist, -1); |
| 41 | + for (int i = 0; i <= N; i++) { |
| 42 | + adj.add(new ArrayList<>()); |
| 43 | + } |
| 44 | + |
| 45 | + for (int i = 0; i < M; i++) { |
| 46 | + st = new StringTokenizer(br.readLine()); |
| 47 | + int a = Integer.parseInt(st.nextToken()); |
| 48 | + int b = Integer.parseInt(st.nextToken()); |
| 49 | + int c = Integer.parseInt(st.nextToken()); |
| 50 | + adj.get(a).add(new Node(b, c)); |
| 51 | + adj.get(b).add(new Node(a, c)); |
| 52 | + } |
| 53 | + st = new StringTokenizer(br.readLine()); |
| 54 | + start = Integer.parseInt(st.nextToken()); |
| 55 | + end = Integer.parseInt(st.nextToken()); |
| 56 | + |
| 57 | + System.out.println(dijskra()); |
| 58 | + } |
| 59 | + |
| 60 | + private static class Node implements Comparable<Node>{ |
| 61 | + int idx, c; |
| 62 | + |
| 63 | + public Node(int u, int c) { |
| 64 | + this.idx = u; |
| 65 | + this.c = c; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public int compareTo(Node o) { |
| 70 | + return o.c - this.c; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments