Skip to content

Commit da9b12d

Browse files
committed
[Silver I] Title: 지름길, Time: 116 ms, Memory: 14504 KB -BaekjoonHub
1 parent 65f7c41 commit da9b12d

2 files changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [Silver I] 지름길 - 1446
2+
3+
[문제 링크](https://www.acmicpc.net/problem/1446)
4+
5+
### 성능 요약
6+
7+
메모리: 14504 KB, 시간: 116 ms
8+
9+
### 분류
10+
11+
데이크스트라, 다이나믹 프로그래밍, 그래프 이론, 최단 경로
12+
13+
### 제출 일자
14+
15+
2025년 4월 15일 14:01:36
16+
17+
### 문제 설명
18+
19+
<p>매일 아침, 세준이는 학교에 가기 위해서 차를 타고 D킬로미터 길이의 고속도로를 지난다. 이 고속도로는 심각하게 커브가 많아서 정말 운전하기도 힘들다. 어느 날, 세준이는 이 고속도로에 지름길이 존재한다는 것을 알게 되었다. 모든 지름길은 일방통행이고, 고속도로를 역주행할 수는 없다.</p>
20+
21+
<p>세준이가 운전해야 하는 거리의 최솟값을 출력하시오.</p>
22+
23+
### 입력
24+
25+
<p>첫째 줄에 지름길의 개수 N과 고속도로의 길이 D가 주어진다. N은 12 이하인 양의 정수이고, D는 10,000보다 작거나 같은 자연수이다. 다음 N개의 줄에 지름길의 시작 위치, 도착 위치, 지름길의 길이가 주어진다. 모든 위치와 길이는 10,000보다 작거나 같은 음이 아닌 정수이다. 지름길의 시작 위치는 도착 위치보다 작다.</p>
26+
27+
### 출력
28+
29+
<p>세준이가 운전해야하는 거리의 최솟값을 출력하시오.</p>
30+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static PriorityQueue<node> q = new PriorityQueue<>(
6+
(o1, o2) -> {
7+
if(o1.u == o2.u || o1.v == o2.v) return o1.reduce - o2.reduce;
8+
else return o1.u - o2.u;
9+
});
10+
11+
static int INF = 1200001;
12+
static int n, d;
13+
static int[] memo;
14+
static List<node>[] graph;
15+
static StringTokenizer st;
16+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
public static void main(String[] args) throws Exception{
18+
inputSetting();
19+
findMinLen();
20+
System.out.println(memo[d]);
21+
}
22+
23+
static void findMinLen(){
24+
node now, next;
25+
while(!q.isEmpty()){
26+
now = q.poll();
27+
28+
if(memo[now.v] < memo[now.u] + now.l) continue;
29+
memo[now.v] = memo[now.u] + now.l;
30+
31+
if(d <= now.v || memo[now.v + 1] < memo[now.v] + 1) continue;
32+
q.add(new node(now.v, now.v + 1, 1));
33+
}
34+
}
35+
36+
static void inputSetting() throws Exception{
37+
st = new StringTokenizer(br.readLine());
38+
39+
n = Integer.parseInt(st.nextToken());
40+
d = Integer.parseInt(st.nextToken());
41+
42+
memo = new int [d + 1];
43+
for(int i = 0; i < d + 1; i++) memo[i] = i;
44+
45+
int u, v, l;
46+
for(int i = 0; i < n; i++){
47+
st = new StringTokenizer(br.readLine());
48+
49+
u = Integer.parseInt(st.nextToken());
50+
v = Integer.parseInt(st.nextToken());
51+
l = Integer.parseInt(st.nextToken());
52+
53+
if(d < u || d < v) continue;
54+
55+
q.add(new node(u, v, l));
56+
}
57+
}
58+
static class node{
59+
int u, v, origin, reduce,l;
60+
node(int u, int v, int l){
61+
this.u = u;
62+
this.v = v;
63+
this.l = l;
64+
origin = v - u;
65+
reduce = l - this.origin;
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)