|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class BJ_14938_서강그라운드 { |
| 6 | + |
| 7 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 9 | + private static StringTokenizer st; |
| 10 | + |
| 11 | + private static int N, M, R, tmp, ans; |
| 12 | + private static int[] dist, vals; |
| 13 | + private static List<Node>[] graph; |
| 14 | + private static Queue<Node> q; |
| 15 | + |
| 16 | + private static class Node implements Comparable<Node> { |
| 17 | + int to; |
| 18 | + int dist; |
| 19 | + |
| 20 | + public Node(int to, int dist) { |
| 21 | + this.to = to; |
| 22 | + this.dist = dist; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public int compareTo(Node o) { |
| 27 | + return Integer.compare(this.dist, o.dist); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public static void main(String[] args) throws IOException { |
| 32 | + init(); |
| 33 | + sol(); |
| 34 | + } |
| 35 | + |
| 36 | + private static void init() throws IOException { |
| 37 | + st = new StringTokenizer(br.readLine()); |
| 38 | + |
| 39 | + N = Integer.parseInt(st.nextToken()); |
| 40 | + M = Integer.parseInt(st.nextToken()); |
| 41 | + R = Integer.parseInt(st.nextToken()); |
| 42 | + |
| 43 | + ans = 0; |
| 44 | + vals = new int[N + 1]; |
| 45 | + st = new StringTokenizer(br.readLine()); |
| 46 | + for (int i = 1; i <= N; i++) { |
| 47 | + vals[i] = Integer.parseInt(st.nextToken()); |
| 48 | + } |
| 49 | + |
| 50 | + graph = new List[N + 1]; |
| 51 | + for (int i = 1; i <= N; i++) { |
| 52 | + graph[i] = new ArrayList<>(); |
| 53 | + } |
| 54 | + |
| 55 | + for (int i = 0; i < R; i++) { |
| 56 | + st = new StringTokenizer(br.readLine()); |
| 57 | + |
| 58 | + int u = Integer.parseInt(st.nextToken()); |
| 59 | + int v = Integer.parseInt(st.nextToken()); |
| 60 | + int w = Integer.parseInt(st.nextToken()); |
| 61 | + |
| 62 | + graph[u].add(new Node(v, w)); |
| 63 | + graph[v].add(new Node(u, w)); |
| 64 | + } |
| 65 | + |
| 66 | + q = new PriorityQueue<>(); |
| 67 | + dist = new int[N + 1]; |
| 68 | + } |
| 69 | + |
| 70 | + private static void sol() throws IOException { |
| 71 | + for (int i = 1; i <= N; i++) { |
| 72 | + dijkstra(i); |
| 73 | + |
| 74 | + tmp = 0; |
| 75 | + for (int j = 1; j <= N; j++) { |
| 76 | + if (dist[j] <= M) { |
| 77 | + tmp += vals[j]; |
| 78 | + } |
| 79 | + } |
| 80 | + ans = Math.max(ans, tmp); |
| 81 | + } |
| 82 | + bw.write(ans + "\n"); |
| 83 | + bw.flush(); |
| 84 | + bw.close(); |
| 85 | + br.close(); |
| 86 | + } |
| 87 | + |
| 88 | + private static void dijkstra(int start) { |
| 89 | + q.clear(); |
| 90 | + Arrays.fill(dist, Integer.MAX_VALUE); |
| 91 | + dist[start] = 0; |
| 92 | + |
| 93 | + q.offer(new Node(start, 0)); |
| 94 | + |
| 95 | + while (!q.isEmpty()) { |
| 96 | + Node cur = q.poll(); |
| 97 | + |
| 98 | + if (dist[cur.to] < cur.dist) continue; |
| 99 | + |
| 100 | + for (Node next : graph[cur.to]) { |
| 101 | + int newDist = dist[cur.to] + next.dist; |
| 102 | + |
| 103 | + if (newDist < dist[next.to]) { |
| 104 | + dist[next.to] = newDist; |
| 105 | + q.offer(new Node(next.to, newDist)); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | +} |
| 112 | +``` |
0 commit comments