Skip to content

Commit 6f50ccb

Browse files
authored
[20250715] BOJ / G4 / 서강그라운드 / 신동윤
1 parent 7c1f1ea commit 6f50ccb

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static int n, m, r, maxItem;
7+
static int[] items;
8+
static List<int[]>[] graph;
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
n = Integer.parseInt(st.nextToken());
13+
m = Integer.parseInt(st.nextToken());
14+
r = Integer.parseInt(st.nextToken());
15+
items = new int[n+1];
16+
st = new StringTokenizer(br.readLine());
17+
for (int i = 1; i <= n; i++) {
18+
items[i] = Integer.parseInt(st.nextToken());
19+
}
20+
graph = new List[n+1];
21+
for (int i = 1; i <= n; i++) {
22+
graph[i] = new ArrayList<>();
23+
}
24+
for (int i = 0; i < r; i++) {
25+
st = new StringTokenizer(br.readLine());
26+
int a = Integer.parseInt(st.nextToken());
27+
int b = Integer.parseInt(st.nextToken());
28+
int length = Integer.parseInt(st.nextToken());
29+
graph[a].add(new int[] {b, length});
30+
graph[b].add(new int[] {a, length});
31+
}
32+
33+
for (int start = 1; start <= n; start++) {
34+
dijkstra(start);
35+
}
36+
System.out.println(maxItem);
37+
br.close();
38+
}
39+
40+
private static void dijkstra(int start) {
41+
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
42+
@Override
43+
public int compare(int[] o1, int[] o2) {
44+
return Integer.compare(o1[1], o2[1]);
45+
}
46+
});
47+
48+
int[] dist = new int[n+1];
49+
for (int i = 1; i <= n; i++) {
50+
dist[i] = Integer.MAX_VALUE;
51+
}
52+
dist[start] = 0;
53+
pq.offer(new int[] {start, 0});
54+
55+
while (!pq.isEmpty()) {
56+
int[] cur = pq.poll();
57+
int curNode = cur[0];
58+
for (int[] nex : graph[curNode]) {
59+
int nextNode = nex[0];
60+
int nextLength = nex[1];
61+
if (dist[nextNode] > dist[curNode] + nextLength) {
62+
if (dist[curNode] + nextLength > m) {
63+
continue;
64+
}
65+
dist[nextNode] = dist[curNode] + nextLength;
66+
pq.offer(new int[]{nextNode, dist[nextNode]});
67+
}
68+
}
69+
}
70+
// 각 정점까지의 최단거리 구하기 완료
71+
// start 정점에서 출발했을 떄, 얻을 수 있는 최대 아이템 개수 출력
72+
int tmpItem = 0;
73+
for (int i = 1; i <= n; i++) {
74+
if (dist[i] == Integer.MAX_VALUE) { continue; }
75+
tmpItem += items[i];
76+
}
77+
maxItem = Math.max(tmpItem, maxItem);
78+
}
79+
}
80+
```

0 commit comments

Comments
 (0)