|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class BJ_1939_중량제한 { |
| 6 | + |
| 7 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 9 | + private static StringTokenizer st; |
| 10 | + |
| 11 | + private static int N, M, start, end; |
| 12 | + private static int[] maxWeight; |
| 13 | + private static List<Node>[] graph; |
| 14 | + private static Queue<Node> pq; |
| 15 | + |
| 16 | + private static class Node implements Comparable<Node> { |
| 17 | + int to; |
| 18 | + int weight; |
| 19 | + |
| 20 | + public Node(int to, int weight) { |
| 21 | + this.to = to; |
| 22 | + this.weight = weight; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public int compareTo(Node o) { |
| 27 | + return Integer.compare(o.weight, this.weight); |
| 28 | + } |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + public static void main(String[] args) throws IOException { |
| 33 | + init(); |
| 34 | + sol(); |
| 35 | + } |
| 36 | + |
| 37 | + private static void init() throws IOException { |
| 38 | + st = new StringTokenizer(br.readLine()); |
| 39 | + N = Integer.parseInt(st.nextToken()); |
| 40 | + M = Integer.parseInt(st.nextToken()); |
| 41 | + |
| 42 | + graph = new List[N + 1]; |
| 43 | + for (int i = 1; i <= N; i++) { |
| 44 | + graph[i] = new ArrayList<>(); |
| 45 | + } |
| 46 | + |
| 47 | + for (int i = 1; i <= M; i++) { |
| 48 | + st = new StringTokenizer(br.readLine()); |
| 49 | + |
| 50 | + int a = Integer.parseInt(st.nextToken()); |
| 51 | + int b = Integer.parseInt(st.nextToken()); |
| 52 | + int c = Integer.parseInt(st.nextToken()); |
| 53 | + |
| 54 | + graph[a].add(new Node(b, c)); |
| 55 | + graph[b].add(new Node(a, c)); |
| 56 | + } |
| 57 | + |
| 58 | + maxWeight = new int[N + 1]; |
| 59 | + |
| 60 | + st = new StringTokenizer(br.readLine()); |
| 61 | + start = Integer.parseInt(st.nextToken()); |
| 62 | + end = Integer.parseInt(st.nextToken()); |
| 63 | + maxWeight[start] = Integer.MAX_VALUE; |
| 64 | + |
| 65 | + pq = new PriorityQueue<>(); |
| 66 | + } |
| 67 | + |
| 68 | + private static void sol() throws IOException { |
| 69 | + pq.offer(new Node(start, Integer.MAX_VALUE)); |
| 70 | + |
| 71 | + while (!pq.isEmpty()) { |
| 72 | + Node cur = pq.poll(); |
| 73 | + |
| 74 | + if (cur.to == end) { |
| 75 | + bw.write(String.valueOf(cur.weight)); |
| 76 | + bw.flush(); |
| 77 | + bw.close(); |
| 78 | + br.close(); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + if (cur.weight < maxWeight[cur.to]) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + for (Node next : graph[cur.to]) { |
| 87 | + int newWeight = Math.min(cur.weight, next.weight); |
| 88 | + |
| 89 | + if (newWeight > maxWeight[next.to]) { |
| 90 | + maxWeight[next.to] = newWeight; |
| 91 | + pq.offer(new Node(next.to, newWeight)); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | +} |
| 98 | +``` |
0 commit comments