
소스코드:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br;
static BufferedWriter bw;
static int maxItemCount;
static int NODE_COUNT;
static int NEAR;
static int WAY_COUNT;
static int[] ITEM;
static List<List<Node>> nodeList;
public static class Node {
int to;
int cost;
Node(int t, int c) {
this.to = t;
this.cost = c;
}
}
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
NODE_COUNT = Integer.parseInt(st.nextToken());
NEAR = Integer.parseInt(st.nextToken());
WAY_COUNT = Integer.parseInt(st.nextToken());
ITEM = new int[NODE_COUNT + 1];
nodeList = new ArrayList<>();
for (int i = 0; i <= NODE_COUNT; i++) {
nodeList.add(new ArrayList<>());
}
st = new StringTokenizer(br.readLine());
for (int i = 1; i <= NODE_COUNT; i++) {
ITEM[i] = Integer.parseInt(st.nextToken());
}
for (int i = 1; i <= WAY_COUNT; i++) {
st = new StringTokenizer(br.readLine());
int cur = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
nodeList.get(cur).add(new Node(to, cost));
nodeList.get(to).add(new Node(cur, cost));
}
br.close();
for (int i = 1; i <= NODE_COUNT; i++) {
maxItemCount = Math.max(maxItemCount, dijkstra(i));
}
bw.write(maxItemCount + "\n");
bw.close();
}
public static int dijkstra(int start) {
PriorityQueue<Node> pq = new PriorityQueue<>((a, b) -> a.cost - b.cost);
int[] dist = new int[NODE_COUNT + 1];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[start] = 0;
pq.add(new Node(start, 0));
while (!pq.isEmpty()) {
Node cur = pq.poll();
if (dist[cur.to] < cur.cost) {
continue;
}
for (Node next : nodeList.get(cur.to)) {
int nextNode = next.to;
int nextCost = cur.cost + next.cost;
if (dist[nextNode] > nextCost) {
dist[nextNode] = nextCost;
pq.add(new Node(nextNode, nextCost));
}
}
}
int total = 0;
for (int i = 1; i <= NODE_COUNT; i++) {
if (dist[i] <= NEAR) {
total += ITEM[i];
}
}
return total;
}
}
글의 내용 중 잘못된 점이나 수정이 필요한 부분, 혹은 궁금한 사항이 있다면 언제든 댓글로 남겨주시면 감사하겠습니다.
여러분의 피드백은 더 나은 글을 작성하는 데 큰 도움이 됩니다. 감사합니다.
'알고리즘 > 백준' 카테고리의 다른 글
| [JAVA-자바] 1238번: 파티 (0) | 2025.05.19 |
|---|---|
| [JAVA-자바] 17144번: 미세먼지 안녕! (0) | 2025.05.18 |
| [JAVA-자바] 14502번: 연구소 (0) | 2025.05.15 |
| [JAVA-자바] 12851번: 숨바꼭질 2 (0) | 2025.05.14 |
| [JAVA-자바] 11404번: 플로이드 (0) | 2025.05.13 |