Skip to content

Commit 86ea09b

Browse files
committed
[Gold V] Title: 택배 배송, Time: 368 ms, Memory: 40032 KB -BaekjoonHub
1 parent d83f4ad commit 86ea09b

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

백준/Gold/5972. 택배 배송/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 40400 KB, 시간: 392 ms
7+
메모리: 40032 KB, 시간: 368 ms
88

99
### 분류
1010

11-
데이크스트라, 그래프 이론, 최단 경로
11+
그래프 이론, 최단 경로, 데이크스트라
1212

1313
### 제출 일자
1414

15-
2025년 5월 8일 18:09:25
15+
2025년 5월 30일 15:31:14
1616

1717
### 문제 설명
1818

백준/Gold/5972. 택배 배송/택배 배송.java

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,74 @@
22
import java.util.*;
33

44
public class Main {
5-
65
private static int N,M;
7-
private static int[] distance;
6+
private static boolean[] visit;
7+
private static int[] dis;
8+
89
private static class Node implements Comparable<Node>{
9-
int t;
10-
int weight;
10+
int to;
11+
int cost;
1112

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;
1516
}
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;
1919
}
2020
}
2121

22-
private static ArrayList<ArrayList<Node>> graph = new ArrayList<>();
23-
22+
private static ArrayList<Node>[] graph;
2423
public static void main(String[] args) throws IOException{
2524
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
2625
StringTokenizer st = new StringTokenizer(br.readLine());
26+
2727
N = Integer.parseInt(st.nextToken());
2828
M = Integer.parseInt(st.nextToken());
2929

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;
3437
}
35-
distance[1] = 0;
38+
3639
for(int i = 0; i < M; i++){
3740
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());
4141

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));
4448
}
4549

46-
find();
47-
System.out.print(distance[N]);
50+
find(1);
51+
System.out.print(dis[N]);
4852
}
4953

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();
5361

54-
while(!queue.isEmpty()){
55-
Node now = queue.poll();
62+
if(visit[temp.to]) continue;
63+
visit[temp.to] = true;
5664

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]));
6270
}
6371
}
72+
}
73+
6474
}
6575
}

0 commit comments

Comments
 (0)