|
2 | 2 | import java.util.*; |
3 | 3 |
|
4 | 4 | public class Main { |
5 | | - |
6 | 5 | private static int N,M; |
7 | | - private static int[] distance; |
| 6 | + private static boolean[] visit; |
| 7 | + private static int[] dis; |
| 8 | + |
8 | 9 | private static class Node implements Comparable<Node>{ |
9 | | - int t; |
10 | | - int weight; |
| 10 | + int to; |
| 11 | + int cost; |
11 | 12 |
|
12 | | - public Node(int t, int weight){ |
13 | | - this.t = t; |
14 | | - this.weight = weight; |
| 13 | + public Node(int to, int cost){ |
| 14 | + this.to = to; |
| 15 | + this.cost = cost; |
15 | 16 | } |
16 | | - |
17 | | - public int compareTo(Node node){ |
18 | | - return this.weight - node.weight; |
| 17 | + public int compareTo(Node t){ |
| 18 | + return this.cost - t.cost; |
19 | 19 | } |
20 | 20 | } |
21 | 21 |
|
22 | | - private static ArrayList<ArrayList<Node>> graph = new ArrayList<>(); |
23 | | - |
| 22 | + private static ArrayList<Node>[] graph; |
24 | 23 | public static void main(String[] args) throws IOException{ |
25 | 24 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
26 | 25 | StringTokenizer st = new StringTokenizer(br.readLine()); |
| 26 | + |
27 | 27 | N = Integer.parseInt(st.nextToken()); |
28 | 28 | M = Integer.parseInt(st.nextToken()); |
29 | 29 |
|
30 | | - distance = new int[N+1]; |
31 | | - for(int i = 0; i < N+1; i++){ |
32 | | - graph.add(new ArrayList<>()); |
33 | | - distance[i] = Integer.MAX_VALUE; |
| 30 | + graph = new ArrayList[N+1]; |
| 31 | + dis = new int[N+1]; |
| 32 | + visit = new boolean[N+1]; |
| 33 | + |
| 34 | + for(int i = 0; i <= N; i++){ |
| 35 | + graph[i] = new ArrayList<>(); |
| 36 | + dis[i] = Integer.MAX_VALUE; |
34 | 37 | } |
35 | | - distance[1] = 0; |
| 38 | + |
36 | 39 | for(int i = 0; i < M; i++){ |
37 | 40 | st = new StringTokenizer(br.readLine()); |
38 | | - int a = Integer.parseInt(st.nextToken()); |
39 | | - int b = Integer.parseInt(st.nextToken()); |
40 | | - int c = Integer.parseInt(st.nextToken()); |
41 | 41 |
|
42 | | - graph.get(a).add(new Node(b,c)); |
43 | | - graph.get(b).add(new Node(a,c)); |
| 42 | + int from = Integer.parseInt(st.nextToken()); |
| 43 | + int to = Integer.parseInt(st.nextToken()); |
| 44 | + int cost = Integer.parseInt(st.nextToken()); |
| 45 | + |
| 46 | + graph[from].add(new Node(to,cost)); |
| 47 | + graph[to].add(new Node(from,cost)); |
44 | 48 | } |
45 | 49 |
|
46 | | - find(); |
47 | | - System.out.print(distance[N]); |
| 50 | + find(1); |
| 51 | + System.out.print(dis[N]); |
48 | 52 | } |
49 | 53 |
|
50 | | - private static void find(){ |
51 | | - PriorityQueue<Node> queue = new PriorityQueue<>(); |
52 | | - queue.add(new Node(1,0)); |
| 54 | + private static void find(int start){ |
| 55 | + PriorityQueue<Node> queue = new PriorityQueue<>(); |
| 56 | + queue.add(new Node(start,0)); |
| 57 | + dis[start] = 0; |
| 58 | + |
| 59 | + while(!queue.isEmpty()){ |
| 60 | + Node temp = queue.poll(); |
53 | 61 |
|
54 | | - while(!queue.isEmpty()){ |
55 | | - Node now = queue.poll(); |
| 62 | + if(visit[temp.to]) continue; |
| 63 | + visit[temp.to] = true; |
56 | 64 |
|
57 | | - for(Node next : graph.get(now.t)){ |
58 | | - if(distance[next.t] > distance[now.t] + next.weight){ |
59 | | - distance[next.t] = distance[now.t] + next.weight; |
60 | | - queue.add(new Node(next.t, distance[next.t])); |
61 | | - } |
| 65 | + for(Node next : graph[temp.to]){ |
| 66 | + if(dis[next.to] > dis[temp.to] + next.cost){ |
| 67 | + dis[next.to] = dis[temp.to] + next.cost; |
| 68 | + |
| 69 | + queue.add(new Node(next.to, dis[next.to])); |
62 | 70 | } |
63 | 71 | } |
| 72 | + } |
| 73 | + |
64 | 74 | } |
65 | 75 | } |
0 commit comments