結果
問題 | No.1473 おでぶなおばけさん |
ユーザー | shojin_pro |
提出日時 | 2021-04-10 01:09:26 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,097 ms / 2,000 ms |
コード長 | 8,160 bytes |
コンパイル時間 | 3,022 ms |
コンパイル使用メモリ | 86,728 KB |
実行使用メモリ | 91,328 KB |
最終ジャッジ日時 | 2024-06-25 13:55:53 |
合計ジャッジ時間 | 36,138 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 90 ms
38,944 KB |
testcase_01 | AC | 101 ms
39,224 KB |
testcase_02 | AC | 1,031 ms
83,580 KB |
testcase_03 | AC | 828 ms
71,568 KB |
testcase_04 | AC | 643 ms
67,656 KB |
testcase_05 | AC | 379 ms
53,380 KB |
testcase_06 | AC | 868 ms
73,036 KB |
testcase_07 | AC | 1,062 ms
82,744 KB |
testcase_08 | AC | 1,097 ms
91,328 KB |
testcase_09 | AC | 1,078 ms
90,556 KB |
testcase_10 | AC | 560 ms
52,572 KB |
testcase_11 | AC | 544 ms
52,420 KB |
testcase_12 | AC | 553 ms
53,460 KB |
testcase_13 | AC | 438 ms
50,276 KB |
testcase_14 | AC | 369 ms
50,452 KB |
testcase_15 | AC | 511 ms
52,988 KB |
testcase_16 | AC | 550 ms
53,020 KB |
testcase_17 | AC | 241 ms
46,700 KB |
testcase_18 | AC | 264 ms
48,156 KB |
testcase_19 | AC | 518 ms
55,316 KB |
testcase_20 | AC | 797 ms
70,236 KB |
testcase_21 | AC | 707 ms
60,196 KB |
testcase_22 | AC | 870 ms
82,616 KB |
testcase_23 | AC | 795 ms
74,372 KB |
testcase_24 | AC | 717 ms
69,116 KB |
testcase_25 | AC | 810 ms
79,844 KB |
testcase_26 | AC | 752 ms
70,860 KB |
testcase_27 | AC | 410 ms
54,596 KB |
testcase_28 | AC | 927 ms
84,224 KB |
testcase_29 | AC | 562 ms
58,608 KB |
testcase_30 | AC | 628 ms
65,140 KB |
testcase_31 | AC | 904 ms
84,372 KB |
testcase_32 | AC | 696 ms
69,796 KB |
testcase_33 | AC | 651 ms
67,448 KB |
testcase_34 | AC | 433 ms
57,780 KB |
testcase_35 | AC | 419 ms
54,420 KB |
testcase_36 | AC | 592 ms
64,420 KB |
testcase_37 | AC | 699 ms
71,068 KB |
testcase_38 | AC | 295 ms
49,104 KB |
testcase_39 | AC | 545 ms
52,344 KB |
testcase_40 | AC | 549 ms
52,552 KB |
testcase_41 | AC | 539 ms
50,732 KB |
testcase_42 | AC | 531 ms
51,644 KB |
testcase_43 | AC | 556 ms
67,788 KB |
testcase_44 | AC | 561 ms
66,560 KB |
testcase_45 | AC | 536 ms
67,676 KB |
testcase_46 | AC | 805 ms
70,124 KB |
testcase_47 | AC | 914 ms
79,572 KB |
testcase_48 | AC | 877 ms
80,236 KB |
ソースコード
import java.util.*; import java.io.*; public class Main { static FastScanner sc = new FastScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); static ArrayList<ArrayList<Edge>> G = new ArrayList<>(); static ArrayList<Edge> arr = new ArrayList<>(); public static void main(String[] args) throws Exception { solve(); pw.flush(); } public static void solve() { int N = sc.nextInt(); int E = sc.nextInt(); DSU dsu = new DSU(N); for(int i = 0; i < N; i++){ G.add(new ArrayList<>()); } for (int i = 0; i < E; i++) { int a = sc.nextInt()-1; int b = sc.nextInt()-1; long d = sc.nextLong(); arr.add(new Edge(a,b,d)); } Collections.sort(arr); long ans = 0L; for(int i = 0; i < E; i++){ Edge e = arr.get(i); if(ans > e.max){ break; } //pw.println(e.from +" " + e.to + " " + e.max); dsu.merge(e.from,e.to); G.get(e.from).add(new Edge(e.from,e.to,e.max)); G.get(e.to).add(new Edge(e.to,e.from,e.max)); if(dsu.same(0,N-1)){ ans = e.max; } } PriorityQueue<Point> pq = new PriorityQueue<>(); pq.add(new Point(0,0)); int[] min = new int[N]; Arrays.fill(min,(int)1e10); min[0] = 0; while(pq.size() > 0){ Point p = pq.poll(); if(p.now == N-1){ pw.println(ans + " " + p.cnt); return; } if(min[p.now] < p.cnt){ continue; } for(Edge e : G.get(p.now)){ if(min[e.to] > p.cnt+1){ min[e.to] = p.cnt+1; pq.add(new Point(e.to,p.cnt+1)); } } } } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } static class Point implements Comparable<Point>{ int now, cnt; public Point(int now, int cnt) { this.now = now; this.cnt = cnt; } public int compareTo(Point p){ if(this.cnt < p.cnt){ return -1; }else if(this.cnt > p.cnt){ return 1; }else{ return 0; } } } static class Edge implements Comparable<Edge>{ int from, to; long max; public Edge(int from, int to, long max) { this.from = from; this.to = to; this.max = max; } public int compareTo(Edge e){ if(this.max > e.max){ return -1; }else if(this.max < e.max){ return 1; }else{ return 0; } } } } class DSU { private int n; private int[] parentOrSize; private java.util.ArrayList<java.util.ArrayList<Integer>> map; public DSU(int n) { this.n = n; this.map = new java.util.ArrayList<java.util.ArrayList<Integer>>(); for (int i = 0; i < n; i++) { this.map.add(new java.util.ArrayList<Integer>()); this.map.get(i).add(i); } this.parentOrSize = new int[n]; java.util.Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; this.map.get(x).addAll(this.map.get(y)); return x; } boolean same(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); return leader(a) == leader(b); } int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("" + a); return -parentOrSize[leader(a)]; } java.util.ArrayList<java.util.ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } java.util.ArrayList<java.util.ArrayList<Integer>> result = new java.util.ArrayList<>(n); for (int i = 0; i < n; i++) { result.add(new java.util.ArrayList<>(groupSize[i])); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } result.removeIf(java.util.ArrayList::isEmpty); return result; } java.util.ArrayList<Integer> getArray(int n) { return this.map.get(n); } } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public FastScanner(FileReader in) { reader = new BufferedReader(in); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String[] nextArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } }