-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra_01446_shortWay.java
94 lines (76 loc) · 2.56 KB
/
dijkstra_01446_shortWay.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package dijkstra;
import java.io.*;
import java.util.*;
/**
* 1. 문제 링크 : https://www.acmicpc.net/problem/1446
* 2. 풀이
* - dijkstra에 앞서 PriorityQueue로 한 번 풀어봄
* >> dijkstra로 다시 풀어라~
*/
public class dijkstra_01446_shortWay {
static final int INF = 987654321;
static int D;
static int[] dist;
static Map<Integer, List<Node>> map;
static void solve() {
dist = new int[D + 1];
Arrays.fill(dist, INF);
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(0, 0));
dist[0] = 0;
while(!pq.isEmpty()) {
Node now = pq.poll();
int next_1 = now.pos + 1;
if(next_1 > D) continue;
if(now.dist + 1 < dist[next_1]) {
dist[next_1] = now.dist + 1;
pq.add(new Node(next_1, now.dist + 1));
}
if(map.containsKey(now.pos)) {
for(Node next : map.get(now.pos)) {
if(now.dist + next.dist < dist[next.pos]) {
dist[next.pos] = now.dist + next.dist;
pq.add(new Node(next.pos, now.dist + next.dist));
}
}
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
D = Integer.parseInt(st.nextToken());
map = new HashMap<>();
while(N-- > 0) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
if(D < b) continue;
List<Node> tempLi = map.getOrDefault(a, new ArrayList<>());
tempLi.add(new Node(b, c));
map.put(a, tempLi);
}
solve();
bw.write(dist[D] + "");
bw.flush();
bw.close();
br.close();
}
static class Node implements Comparable<Node> {
int pos;
int dist;
public Node(int pos, int dist) {
this.pos = pos;
this.dist = dist;
}
@Override
public int compareTo(Node n) {
if(n.pos == this.pos)
return this.dist - n.dist;
return n.pos - this.pos;
}
}
}