Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 60f403d

Browse files
committed
배수빈: [BOJ] 9694 무엇을 아느냐가 아니라 누구를 아느냐가 문제다_250406
1 parent f5fac54 commit 60f403d

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

‎BOJ/5001-10000번/SB_9694.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class SB_9694 {
7+
static ArrayList<ArrayList<Node>> adj;
8+
static int[] dist;
9+
static int[] path;
10+
static Integer INF = 987654321;
11+
12+
private static int[] dijsktra(int M) {
13+
dist[0] = 0;
14+
15+
PriorityQueue<Node> pq = new PriorityQueue<>();
16+
pq.offer(new Node(0, 0));
17+
18+
while (!pq.isEmpty()) {
19+
Node cur = pq.poll();
20+
if (cur.cost > dist[cur.idx]) continue;
21+
22+
for (Node nxt : adj.get(cur.idx)) {
23+
if (nxt.cost + dist[cur.idx] < dist[nxt.idx]) {
24+
dist[nxt.idx] = nxt.cost + dist[cur.idx];
25+
path[nxt.idx] = cur.idx;
26+
pq.offer(new Node(nxt.idx, dist[nxt.idx]));
27+
}
28+
}
29+
}
30+
return path;
31+
}
32+
public static void main(String[] args) throws IOException {
33+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
34+
StringTokenizer st;
35+
StringBuilder sb = new StringBuilder();
36+
37+
int T = Integer.parseInt(br.readLine());
38+
39+
int turn = 1;
40+
while (T-- > 0) {
41+
st = new StringTokenizer(br.readLine());
42+
int N = Integer.parseInt(st.nextToken());
43+
int M = Integer.parseInt(st.nextToken());
44+
45+
dist = new int[M];
46+
Arrays.fill(dist, INF);
47+
path = new int[M];
48+
49+
adj = new ArrayList<>();
50+
for (int i = 0; i < M; i++) {
51+
adj.add(new ArrayList<>());
52+
}
53+
54+
for (int i = 0; i < N; i++) {
55+
st = new StringTokenizer(br.readLine());
56+
int u = Integer.parseInt(st.nextToken());
57+
int v = Integer.parseInt(st.nextToken());
58+
int c = Integer.parseInt(st.nextToken());
59+
60+
adj.get(u).add(new Node(v, c));
61+
adj.get(v).add(new Node(u, c));
62+
}
63+
64+
int[] path = dijsktra(M);
65+
66+
if (dist[M-1]==INF){
67+
sb.append("Case #").append(turn++).append(": ").append("-1\n");
68+
}else{
69+
List<Integer> ans = new ArrayList<>();
70+
int now = M-1;
71+
while (now != 0) {
72+
ans.add(now);
73+
now = path[now];
74+
}
75+
ans.add(0);
76+
Collections.reverse(ans);
77+
78+
sb.append("Case #").append(turn++).append(": ");
79+
for (Integer i : ans) {
80+
sb.append(i).append(" ");
81+
}
82+
sb.append('\n');
83+
}
84+
}
85+
86+
System.out.println(sb);
87+
}
88+
89+
static class Node implements Comparable<Node>{
90+
int idx, cost;
91+
92+
Node(int idx, int cost) {
93+
this.idx = idx;
94+
this.cost = cost;
95+
}
96+
97+
@Override
98+
public int compareTo(Node o) {
99+
return this.cost - o.cost;
100+
}
101+
}
102+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /