結果

問題 No.1473 おでぶなおばけさん
ユーザー tentententen
提出日時 2024-02-08 13:09:46
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,776 bytes
コンパイル時間 5,724 ms
コンパイル使用メモリ 84,748 KB
実行使用メモリ 95,204 KB
最終ジャッジ日時 2024-02-08 13:10:29
合計ジャッジ時間 41,521 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
56,936 KB
testcase_01 AC 121 ms
56,952 KB
testcase_02 AC 1,201 ms
92,032 KB
testcase_03 AC 950 ms
85,448 KB
testcase_04 AC 630 ms
69,956 KB
testcase_05 AC 366 ms
63,956 KB
testcase_06 AC 1,021 ms
85,488 KB
testcase_07 AC 920 ms
83,328 KB
testcase_08 AC 979 ms
83,784 KB
testcase_09 AC 915 ms
83,664 KB
testcase_10 AC 570 ms
66,088 KB
testcase_11 AC 536 ms
66,108 KB
testcase_12 AC 574 ms
64,056 KB
testcase_13 AC 442 ms
63,712 KB
testcase_14 AC 370 ms
63,492 KB
testcase_15 AC 535 ms
66,028 KB
testcase_16 AC 537 ms
66,084 KB
testcase_17 AC 226 ms
62,432 KB
testcase_18 AC 258 ms
63,412 KB
testcase_19 AC 527 ms
65,960 KB
testcase_20 AC 914 ms
83,820 KB
testcase_21 AC 638 ms
68,904 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 790 ms
77,160 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 1,123 ms
95,204 KB
testcase_32 AC 892 ms
84,960 KB
testcase_33 AC 840 ms
83,460 KB
testcase_34 AC 414 ms
67,816 KB
testcase_35 AC 409 ms
65,884 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 548 ms
66,092 KB
testcase_40 AC 544 ms
65,828 KB
testcase_41 AC 511 ms
65,868 KB
testcase_42 AC 548 ms
65,872 KB
testcase_43 AC 556 ms
72,772 KB
testcase_44 AC 594 ms
72,352 KB
testcase_45 AC 555 ms
72,996 KB
testcase_46 AC 727 ms
75,048 KB
testcase_47 AC 1,046 ms
91,052 KB
testcase_48 AC 922 ms
86,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        UnionFindTree uft = new UnionFindTree(n);
        Road weight = IntStream.range(0, m).mapToObj(i -> new Road(sc.nextInt() - 1, sc.nextInt() - 1, sc.nextInt()))
            .sorted()
            .filter(x -> uft.unite(x)).findFirst().orElse(null);
        System.out.println(weight.value + " " + uft.getCost());
    }
    
    static class UnionFindTree {
        int size;
        int[] parents;
        List<List<Integer>> graph;
        
        public UnionFindTree(int size) {
            this.size = size;
            parents = IntStream.range(0, size).map(i -> i).toArray();
            graph = IntStream.range(0,size).mapToObj(i -> new ArrayList<Integer>()).collect(Collectors.toList());
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public boolean unite(Road r) {
            if (!same(0, size - 1)) {
                int left = find(r.left);
                int right = find(r.right);
                if (left != right) {
                    parents[left] = right;
                }
                graph.get(r.left).add(r.right);
                graph.get(r.right).add(r.left);
            }
            return same(0, size - 1);
        }
        
        public int getCost() {
            int[] costs = new int[size];
            Arrays.fill(costs, Integer.MAX_VALUE);
            ArrayDeque<Path> deq = new ArrayDeque<>();
            deq.add(new Path(0, 0));
            while (deq.size() > 0) {
                Path p = deq.poll();
                if (costs[p.idx] <= p.value) {
                    continue;
                }
                costs[p.idx] = p.value;
                for (int x : graph.get(p.idx)) {
                    deq.add(new Path(x, p.value + 1));
                }
            }
            return costs[size - 1];
        }
    }
    
    static class Path {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
    }
    
    static class Road implements Comparable<Road> {
        int left;
        int right;
        int value;
        
        public Road(int left, int right, int value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }
        
        public int compareTo(Road another) {
            return another.value - value;
        }
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0