|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class Solution |
| 6 | +{ |
| 7 | + static ArrayList<ArrayList<Point>> graph; |
| 8 | + static int[] answer; |
| 9 | + |
| 10 | + public static void main(String[] args) throws Exception{ |
| 11 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 13 | + |
| 14 | + int p = Integer.parseInt(st.nextToken()); |
| 15 | + int w = Integer.parseInt(st.nextToken()); |
| 16 | + st = new StringTokenizer(br.readLine()); |
| 17 | + int c = Integer.parseInt(st.nextToken()); |
| 18 | + int v = Integer.parseInt(st.nextToken()); |
| 19 | + answer = new int[p]; |
| 20 | + graph = new ArrayList<>(p+1); |
| 21 | + |
| 22 | + for (int i = 0; i < p; i++) { |
| 23 | + graph.add(new ArrayList<Point>()); |
| 24 | + } |
| 25 | + |
| 26 | + for (int i = 0; i < w; i++) { |
| 27 | + st = new StringTokenizer(br.readLine()); |
| 28 | + int start = Integer.parseInt(st.nextToken()); |
| 29 | + int end = Integer.parseInt(st.nextToken()); |
| 30 | + int weight = Integer.parseInt(st.nextToken()); |
| 31 | + |
| 32 | + graph.get(start).add(new Point(end, weight)); |
| 33 | + graph.get(end).add(new Point(start, weight)); |
| 34 | + } |
| 35 | + |
| 36 | + bfs(c, v); |
| 37 | + System.out.println(answer[v]); |
| 38 | + } |
| 39 | + |
| 40 | + static void bfs(int from, int to) { |
| 41 | + Queue<Point> q = new LinkedList<>(); |
| 42 | + q.offer(new Point(from, 0)); |
| 43 | + answer[from] = Integer.MAX_VALUE; |
| 44 | + while (!q.isEmpty()) { |
| 45 | + Point curr = q.poll(); |
| 46 | + if (curr.e == to) continue; |
| 47 | + for (Point next : graph.get(curr.e)) { |
| 48 | + int nextWieght = Math.min(answer[curr.e], next.w); |
| 49 | + if (answer[next.e] >= nextWieght) continue; |
| 50 | + answer[next.e] = nextWieght; |
| 51 | + q.offer(new Point(next.e, nextWieght)); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + static class Point { |
| 57 | + int e; |
| 58 | + int w; |
| 59 | + public Point(int e, int w) { |
| 60 | + this.e = e; |
| 61 | + this.w = w; |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +``` |
0 commit comments