|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class Main { |
| 8 | + static int cityCnt, busCnt,start,goal; |
| 9 | + static int[] distance; |
| 10 | + static boolean[] visited; |
| 11 | + static Node[] nodes; |
| 12 | + static PriorityQueue<Node> pq = new PriorityQueue<>(); |
| 13 | + |
| 14 | + static class Node implements Comparable<Node>{ |
| 15 | + int num; |
| 16 | + Map<Integer, Integer> costs = new HashMap<>(); |
| 17 | + |
| 18 | + public Node(int num) { |
| 19 | + this.num = num; |
| 20 | + } |
| 21 | + |
| 22 | + @Override |
| 23 | + public int compareTo(Node o) { |
| 24 | + return Integer.compare(distance[this.num], distance[o.num]); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + public static void main(String[] args) throws NumberFormatException, IOException { |
| 30 | + init(); |
| 31 | + process(); |
| 32 | + print(); |
| 33 | + } |
| 34 | + |
| 35 | + public static void init() throws NumberFormatException, IOException { |
| 36 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 37 | + cityCnt = Integer.parseInt(br.readLine()); |
| 38 | + busCnt = Integer.parseInt(br.readLine()); |
| 39 | + nodes = new Node[cityCnt+1]; |
| 40 | + distance = new int[cityCnt+1]; |
| 41 | + visited = new boolean[cityCnt+1]; |
| 42 | + for (int i = 0; i <= cityCnt; i++) { |
| 43 | + nodes[i] = new Node(i); |
| 44 | + } |
| 45 | + |
| 46 | + for (int i = 0; i < busCnt; i++) { |
| 47 | + StringTokenizer st = new StringTokenizer (br.readLine()); |
| 48 | + int from = Integer.parseInt(st.nextToken()); |
| 49 | + int to = Integer.parseInt(st.nextToken()); |
| 50 | + int cost = Integer.parseInt(st.nextToken()); |
| 51 | + |
| 52 | + if (nodes[from].costs.containsKey(to)) { |
| 53 | + int smaller = Math.min(cost, nodes[from].costs.get(to)); |
| 54 | + nodes[from].costs.replace(to, smaller); |
| 55 | + } else { |
| 56 | + nodes[from].costs.put(to, cost); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 62 | + start = Integer.parseInt(st.nextToken()); |
| 63 | + goal = Integer.parseInt(st.nextToken()); |
| 64 | + |
| 65 | + Arrays.fill(distance, Integer.MAX_VALUE/2); |
| 66 | + distance[start] = 0; |
| 67 | + } |
| 68 | + |
| 69 | + public static void process() { |
| 70 | + pq.add(nodes[start]); |
| 71 | + |
| 72 | + while(!pq.isEmpty()) { |
| 73 | + Node n = pq.poll(); |
| 74 | + if (visited[n.num]) continue; |
| 75 | + visited[n.num] = true; |
| 76 | + |
| 77 | + for (int to: n.costs.keySet()) { |
| 78 | + if(visited[to]) continue; |
| 79 | + int cost = n.costs.get(to); |
| 80 | + int nDistance = distance[n.num] + cost; |
| 81 | + if (distance[to] > nDistance) { |
| 82 | + distance[to] = nDistance; |
| 83 | + pq.add(nodes[to]); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + public static void print() { |
| 92 | + System.out.println(distance[goal]); |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
0 commit comments