Skip to content

Commit 1535e0c

Browse files
committed
[20250918] BOJ / G3 / 최소비용 구하기 2 / 김민진
1 parent e311467 commit 1535e0c

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
```java
2+
package etc;
3+
4+
import java.io.*;
5+
import java.util.*;
6+
7+
public class BJ_1779_최소비용_구하기_2 {
8+
9+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
private static final StringBuilder sb = new StringBuilder();
12+
private static StringTokenizer st;
13+
14+
private static int N, M;
15+
private static int start, end;
16+
17+
private static Dist[] dist;
18+
private static List<Node>[] graph;
19+
private static Queue<Node> q;
20+
21+
private static class Node implements Comparable<Node> {
22+
int to;
23+
int weight;
24+
25+
Node(int to, int weight) {
26+
this.to = to;
27+
this.weight = weight;
28+
}
29+
30+
@Override
31+
public int compareTo(Node o) {
32+
return Integer.compare(this.weight, o.weight);
33+
}
34+
}
35+
36+
private static class Dist {
37+
int dist;
38+
List<Integer> cities;
39+
40+
Dist(int dist, int cur) {
41+
this.dist = dist;
42+
this.cities = new ArrayList<>();
43+
this.cities.add(cur);
44+
}
45+
46+
Dist(int dist, List<Integer> cities) {
47+
this.dist = dist;
48+
this.cities = new ArrayList<>(cities);
49+
}
50+
51+
public void add(int v) {
52+
this.cities.add(v);
53+
}
54+
}
55+
56+
public static void main(String[] args) throws IOException {
57+
init();
58+
sol();
59+
}
60+
61+
private static void init() throws IOException {
62+
N = Integer.parseInt(br.readLine());
63+
M = Integer.parseInt(br.readLine());
64+
65+
graph = new List[N + 1];
66+
for (int i = 1; i <= N; i++) {
67+
graph[i] = new ArrayList<>();
68+
}
69+
70+
for (int i = 1; i <= M; i++) {
71+
st = new StringTokenizer(br.readLine());
72+
73+
int u = Integer.parseInt(st.nextToken());
74+
int v = Integer.parseInt(st.nextToken());
75+
int w = Integer.parseInt(st.nextToken());
76+
77+
graph[u].add(new Node(v, w));
78+
}
79+
80+
st = new StringTokenizer(br.readLine());
81+
start = Integer.parseInt(st.nextToken());
82+
end = Integer.parseInt(st.nextToken());
83+
84+
q = new PriorityQueue<>();
85+
dist = new Dist[N + 1];
86+
87+
for (int i = 1; i <= N; i++) {
88+
dist[i] = new Dist(Integer.MAX_VALUE, i);
89+
dist[i].cities.clear();
90+
}
91+
}
92+
93+
private static void sol() throws IOException {
94+
q.offer(new Node(start, 0));
95+
dist[start] = new Dist(0, start);
96+
97+
while (!q.isEmpty()) {
98+
Node cur = q.poll();
99+
100+
if (dist[cur.to].dist < cur.weight) continue;
101+
102+
for (Node n : graph[cur.to]) {
103+
int nDist = dist[cur.to].dist + n.weight;
104+
105+
if (dist[n.to] == null || nDist < dist[n.to].dist) {
106+
dist[n.to] = new Dist(nDist, dist[cur.to].cities);
107+
dist[n.to].add(n.to);
108+
q.offer(new Node(n.to, nDist));
109+
}
110+
}
111+
}
112+
113+
sb.append(dist[end].dist).append("\n");
114+
sb.append(dist[end].cities.size()).append("\n");
115+
for (int city : dist[end].cities) {
116+
sb.append(city).append(" ");
117+
}
118+
bw.write(sb.toString());
119+
bw.flush();
120+
bw.close();
121+
br.close();
122+
}
123+
}
124+
```

0 commit comments

Comments
 (0)