Submission #1779158
Source Code Expand
import java.util.*; import java.io.*; public class Main { private static IO io = new IO(); private static final int INF = 1145141919; private static int n = io.nextInt(); public static void main(String[] args) { int m = io.nextInt(); int d[][] = new int[n][n]; for (int i=0; i<n; i++) Arrays.fill(d[i], INF); // 重複した道があるケースもあるのでまず配列で道を厳選してからリストに入れる for (int i=0; i<m; i++) { int a = io.nextInt()-1; int b = io.nextInt()-1; int c = io.nextInt(); d[a][b] = d[b][a] = Math.min(d[a][b], c); } List<ArrayList<int[]>> list = new ArrayList<ArrayList<int[]>>(); for (int i=0; i<n; i++) list.add(new ArrayList<int[]>()); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (d[i][j]!=INF) { list.get(i).add(new int[] {j, d[i][j]}); } } } // 各頂点を始点として全ての点への最短距離を求め、"一番遠い点への距離"の最小値を求める int ans = INF; for (int i = 0; i < n; i++) ans = Math.min(ans, dijkstra(i, list)); System.out.println(ans); } private static int dijkstra(int s, List<ArrayList<int[]>> list) { int dis[] = new int[n]; // 始点からの最短距離 int ans = 0; // 一番遠い点までの距離 Arrays.fill(dis, INF); // 各頂点までの距離を初期化 dis[s] = 0; // 始点の距離は0 PriorityQueue<int[]> que = new PriorityQueue<int[]>(n, new Comparator<int[]>() { public int compare (int a[], int b[]) { return a[0]-b[0]; // 第1キーで昇順ソート } }); que.add(new int[] {0, s}); while (!que.isEmpty()) { int poll[] = que.poll(); int total = poll[0]; // 移動してきた距離 int now = poll[1]; // いまいる頂点 if (dis[now]<total) continue; int way = list.get(now).size(); // いまいる頂点から出てる辺の数 for (int i=0; i<way; i++) { int go = list.get(now).get(i)[0]; int cost = list.get(now).get(i)[1]; if (dis[now]+cost<dis[go]) { dis[go] = dis[now] + cost; que.offer(new int[] {dis[go], go}); } } } for (int i=0; i<n; i++) if (dis[i]!=INF) ans = Math.max(ans, dis[i]); return ans; } static class IO extends PrintWriter { private final InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; IO() { this(System.in); } IO(InputStream source) { super(System.out); this.in = source; } boolean hasNextByte() { if (ptr < buflen) return true; else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) return false; } return true; } int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } boolean isPrintableChar(int c) {return 33<=c &&c <=126;} void skipUnprintable() {while(hasNextByte() && !isPrintableChar(buffer[ptr]))ptr++;} boolean hasNext() { skipUnprintable(); return hasNextByte(); } long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) throw new NumberFormatException(); while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) return minus ? -n : n; else throw new NumberFormatException(); b = readByte(); } } int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public void close() { super.close(); try { in.close(); } catch (IOException ignored) { } } } }
Submission Info
Submission Time | |
---|---|
Task | D - バスと避けられない運命 |
User | creep04 |
Language | Java8 (OpenJDK 1.8.0) |
Score | 100 |
Code Size | 4967 Byte |
Status | AC |
Exec Time | 591 ms |
Memory | 45440 KB |
Judge Result
Set Name | All | ||
---|---|---|---|
Score / Max Score | 100 / 100 | ||
Status |
|
Set Name | Test Cases |
---|---|
All | sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
sample_01.txt | AC | 70 ms | 21460 KB |
sample_02.txt | AC | 70 ms | 19540 KB |
sample_03.txt | AC | 69 ms | 15956 KB |
test_01.txt | AC | 69 ms | 18516 KB |
test_02.txt | AC | 145 ms | 24092 KB |
test_03.txt | AC | 591 ms | 44492 KB |
test_04.txt | AC | 190 ms | 24708 KB |
test_05.txt | AC | 307 ms | 31048 KB |
test_06.txt | AC | 159 ms | 23000 KB |
test_07.txt | AC | 515 ms | 45440 KB |
test_08.txt | AC | 84 ms | 19156 KB |
test_09.txt | AC | 282 ms | 28412 KB |
test_10.txt | AC | 154 ms | 25696 KB |
test_11.txt | AC | 162 ms | 24104 KB |
test_12.txt | AC | 245 ms | 28076 KB |
test_13.txt | AC | 166 ms | 27864 KB |
test_14.txt | AC | 200 ms | 24660 KB |
test_15.txt | AC | 500 ms | 43240 KB |
test_16.txt | AC | 212 ms | 25000 KB |
test_17.txt | AC | 291 ms | 29488 KB |
test_18.txt | AC | 271 ms | 26672 KB |
test_19.txt | AC | 220 ms | 25632 KB |
test_20.txt | AC | 201 ms | 24456 KB |
test_21.txt | AC | 74 ms | 21332 KB |
test_22.txt | AC | 218 ms | 21556 KB |
test_23.txt | AC | 87 ms | 19412 KB |
test_24.txt | AC | 127 ms | 27260 KB |
test_25.txt | AC | 135 ms | 23824 KB |
test_26.txt | AC | 111 ms | 24020 KB |
test_27.txt | AC | 111 ms | 24020 KB |
test_28.txt | AC | 175 ms | 24108 KB |
test_29.txt | AC | 134 ms | 25660 KB |
test_30.txt | AC | 88 ms | 19540 KB |
test_31.txt | AC | 76 ms | 18260 KB |
test_32.txt | AC | 126 ms | 23880 KB |
test_33.txt | AC | 131 ms | 23056 KB |
test_34.txt | AC | 77 ms | 21332 KB |
test_35.txt | AC | 112 ms | 20052 KB |
test_36.txt | AC | 176 ms | 23644 KB |