diff --git a/code_study/Baekjoon/java/_1753.java b/code_study/Baekjoon/java/_1753.java new file mode 100644 index 0000000..10bab72 --- /dev/null +++ b/code_study/Baekjoon/java/_1753.java @@ -0,0 +1,72 @@ +import java.util.*; + +class Node implements Comparable{ + int nextNode; + int weight; + + Node(int nextNode, int weight){ + this.nextNode = nextNode; + this.weight = weight; + } + + @Override + public int compareTo(Node other){ + return this.weight - other.weight; + } +} + +public class _1753{ + static final int INF = Integer.MAX_VALUE; + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + String[] input = sc.nextLine().split(" "); + int V = Integer.parseInt(input[0]); + int E = Integer.parseInt(input[1]); + int K = Integer.parseInt(sc.nextLine()); + ArrayList> graph = new ArrayList<>(); + for (int i = 0; i <= V; i++) { + graph.add(new ArrayList()); + } + + for(int i=1; i<=E; i++){ + String[] linkInfo = sc.nextLine().split(" "); + int u = Integer.parseInt(linkInfo[0]); + int v = Integer.parseInt(linkInfo[1]); + int w = Integer.parseInt(linkInfo[2]); + graph.get(u).add(new Node(v,w)); + } + sc.close(); + + PriorityQueue PQ = new PriorityQueue<>(); + int[] distance = new int[V+1]; + Arrays.fill(distance, INF); + + distance[K] = 0; + PQ.add(new Node(K, 0)); + + while(!PQ.isEmpty()){ + Node current = PQ.poll(); + int currentNode = current.nextNode; + int currentWeight = current.weight; + + if (distance[currentNode] < currentWeight) { + continue; + } + + for(Node next : graph.get(current.nextNode)){ + int nextNode = next.nextNode; + int nextWeight = next.weight; + + if(distance[nextNode] > distance[currentNode] + nextWeight){ + distance[nextNode] = distance[currentNode] + nextWeight; + PQ.add(new Node(nextNode, distance[nextNode])); + } + } + } + + for(int i=1; i<=V; i++){ + System.out.println(distance[i]==INF ? "INF" : distance[i]); + } + + } +} \ No newline at end of file