|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) nextLine(); |
| 24 | + return st.nextToken(); |
| 25 | + } |
| 26 | + |
| 27 | + int nextInt() throws Exception { |
| 28 | + return Integer.parseInt(nextToken()); |
| 29 | + } |
| 30 | + |
| 31 | + long nextLong() throws Exception { |
| 32 | + return Long.parseLong(nextToken()); |
| 33 | + } |
| 34 | + |
| 35 | + double nextDouble() throws Exception { |
| 36 | + return Double.parseDouble(nextToken()); |
| 37 | + } |
| 38 | + |
| 39 | + void close() throws Exception { |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + } |
| 43 | + |
| 44 | + void write(String content) throws Exception { |
| 45 | + bw.write(content); |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +public class Main { |
| 51 | + |
| 52 | + static IOController io; |
| 53 | + |
| 54 | + // |
| 55 | + |
| 56 | + static final long INF = (long)1e18 + 7; |
| 57 | + |
| 58 | + static int N, M, K; |
| 59 | + static List<int[]>[] graph; |
| 60 | + |
| 61 | + public static void main(String[] args) throws Exception { |
| 62 | + |
| 63 | + io = new IOController(); |
| 64 | + |
| 65 | + init(); |
| 66 | + solve(); |
| 67 | + |
| 68 | + io.close(); |
| 69 | + } |
| 70 | + |
| 71 | + public static void init() throws Exception { |
| 72 | + |
| 73 | + N = io.nextInt(); |
| 74 | + M = io.nextInt(); |
| 75 | + K = io.nextInt(); |
| 76 | + graph = new ArrayList[N+1]; |
| 77 | + for (int i = 1; i <= N; i++) graph[i] = new ArrayList<>(); |
| 78 | + for (int i = 1; i <= M; i++) { |
| 79 | + int a = io.nextInt(); |
| 80 | + int b = io.nextInt(); |
| 81 | + int c = io.nextInt(); |
| 82 | + graph[a].add(new int[]{b,c}); |
| 83 | + graph[b].add(new int[]{a,c}); |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + static void solve() throws Exception { |
| 89 | + |
| 90 | + // 다익스트라 돌리기 |
| 91 | + long[] dist = new long[N+1]; |
| 92 | + Arrays.fill(dist, INF); |
| 93 | + PriorityQueue<long[]> pq = new PriorityQueue<>((a,b) -> Long.compare(a[0],b[0])); |
| 94 | + dist[1] = 0; |
| 95 | + pq.offer(new long[]{0,1}); |
| 96 | + while(!pq.isEmpty()) { |
| 97 | + long[] cur = pq.poll(); |
| 98 | + int n = (int)cur[1]; |
| 99 | + long d = cur[0]; |
| 100 | + if(d > dist[n]) continue; |
| 101 | + for(int[] e : graph[n]) { |
| 102 | + if(dist[e[0]] > d + e[1]) { |
| 103 | + dist[e[0]] = d + e[1]; |
| 104 | + pq.offer(new long[]{dist[e[0]], e[0]}); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // dist 정렬 |
| 110 | + long[] arr = new long[N]; |
| 111 | + for(int i=1;i<=N;i++) arr[i-1] = dist[i]; |
| 112 | + Arrays.sort(arr); |
| 113 | + |
| 114 | + // 단순 그래프를 만족하지 않는 경우 먼저 제외 |
| 115 | + long answer = 0; |
| 116 | + for(int i=1;i<=N;i++) for(int[] e:graph[i]) { |
| 117 | + int j = e[0], c = e[1]; |
| 118 | + if(i<j) continue; |
| 119 | + answer -= Math.max(0L, (K - Math.abs(dist[i] - dist[j]) + 1)); |
| 120 | + } |
| 121 | + |
| 122 | + // 답 구하기 |
| 123 | + long prevSum = 0; |
| 124 | + int lastIndex = 0; |
| 125 | + for(int i=1;i<N;i++) { |
| 126 | + while(arr[i] - arr[lastIndex] > K) { |
| 127 | + prevSum -= arr[lastIndex++]; |
| 128 | + } |
| 129 | + answer += (long)(i-lastIndex)*(K-arr[i]+1) + prevSum; |
| 130 | + prevSum += arr[i]; |
| 131 | + } |
| 132 | + |
| 133 | + io.write(answer + "\n"); |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | +} |
| 138 | +``` |
0 commit comments