20250805 baekjoon
This commit is contained in:
parent
af7c3c145f
commit
da63f73953
72
code_study/Baekjoon/java/_1753.java
Normal file
72
code_study/Baekjoon/java/_1753.java
Normal file
@ -0,0 +1,72 @@
|
||||
import java.util.*;
|
||||
|
||||
class Node implements Comparable<Node>{
|
||||
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<ArrayList<Node>> graph = new ArrayList<>();
|
||||
for (int i = 0; i <= V; i++) {
|
||||
graph.add(new ArrayList<Node>());
|
||||
}
|
||||
|
||||
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<Node> 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]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user