|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.StringTokenizer; |
| 6 | + |
| 7 | +public class Main { |
| 8 | + private static BufferedReader br; |
| 9 | + private static StringTokenizer st; |
| 10 | + private static int n; |
| 11 | + private static int m; |
| 12 | + private static int r; |
| 13 | + private static int[][] dist; |
| 14 | + private static int[] items; |
| 15 | + private static final int INF = 1_000_000; |
| 16 | + |
| 17 | + public static void main(String[] args) throws IOException { |
| 18 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 19 | + String[] temp = br.readLine().split(" "); |
| 20 | + n = Integer.parseInt(temp[0]); |
| 21 | + m = Integer.parseInt(temp[1]); |
| 22 | + r = Integer.parseInt(temp[2]); |
| 23 | + items = new int[n + 1]; |
| 24 | + st = new StringTokenizer(br.readLine()); |
| 25 | + for (int i = 1; i <= n; i++) { |
| 26 | + items[i] = Integer.parseInt(st.nextToken()); |
| 27 | + } |
| 28 | + |
| 29 | + dist = new int[n + 1][n + 1]; |
| 30 | + for (int i = 1; i <= n; i++) { |
| 31 | + for (int j = 1; j <= n; j++) { |
| 32 | + if (i == j) dist[i][j] = 0; |
| 33 | + else dist[i][j] = INF; |
| 34 | + } |
| 35 | + } |
| 36 | + for (int i = 0; i < r; i++) { |
| 37 | + st = new StringTokenizer(br.readLine()); |
| 38 | + int a = Integer.parseInt(st.nextToken()); |
| 39 | + int b = Integer.parseInt(st.nextToken()); |
| 40 | + int c = Integer.parseInt(st.nextToken()); |
| 41 | + dist[a][b] = Math.min(dist[a][b], c); |
| 42 | + dist[b][a] = Math.min(dist[b][a], c); |
| 43 | + } |
| 44 | + //플로이드-워셜 |
| 45 | + for (int k = 1; k <= n; k++) { |
| 46 | + for (int i = 1; i <= n; i++) { |
| 47 | + for (int j = 1; j <= n; j++) { |
| 48 | + if (dist[i][k] != INF && dist[k][j] != INF) { |
| 49 | + dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + int maxItems = 0; |
| 56 | + for (int i = 1; i <= n; i++) { |
| 57 | + int currentItems = 0; |
| 58 | + for (int j = 1; j <= n; j++) { |
| 59 | + if (dist[i][j] <= m) { |
| 60 | + currentItems += items[j]; |
| 61 | + } |
| 62 | + } |
| 63 | + maxItems = Math.max(maxItems, currentItems); |
| 64 | + } |
| 65 | + System.out.println(maxItems); |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
0 commit comments