結果

問題 No.1473 おでぶなおばけさん
ユーザー tentententen
提出日時 2024-02-08 13:09:46
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,776 bytes
コンパイル時間 2,478 ms
コンパイル使用メモリ 84,780 KB
実行使用メモリ 81,936 KB
最終ジャッジ日時 2024-09-28 12:44:07
合計ジャッジ時間 33,010 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
44,256 KB
testcase_01 AC 98 ms
44,248 KB
testcase_02 AC 1,003 ms
81,936 KB
testcase_03 AC 872 ms
72,288 KB
testcase_04 AC 483 ms
59,624 KB
testcase_05 AC 311 ms
50,476 KB
testcase_06 AC 910 ms
73,000 KB
testcase_07 AC 710 ms
68,696 KB
testcase_08 AC 758 ms
68,752 KB
testcase_09 AC 745 ms
68,388 KB
testcase_10 AC 464 ms
51,640 KB
testcase_11 AC 470 ms
51,444 KB
testcase_12 AC 465 ms
51,660 KB
testcase_13 AC 381 ms
50,532 KB
testcase_14 AC 335 ms
49,720 KB
testcase_15 AC 440 ms
51,240 KB
testcase_16 AC 452 ms
51,296 KB
testcase_17 AC 203 ms
46,132 KB
testcase_18 AC 228 ms
48,088 KB
testcase_19 AC 462 ms
52,564 KB
testcase_20 AC 759 ms
70,576 KB
testcase_21 AC 536 ms
55,052 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 667 ms
63,532 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 874 ms
80,940 KB
testcase_32 AC 739 ms
72,020 KB
testcase_33 AC 689 ms
70,284 KB
testcase_34 AC 349 ms
54,032 KB
testcase_35 AC 336 ms
52,448 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 472 ms
51,616 KB
testcase_40 AC 446 ms
51,608 KB
testcase_41 AC 431 ms
51,760 KB
testcase_42 AC 443 ms
51,460 KB
testcase_43 AC 475 ms
59,792 KB
testcase_44 AC 499 ms
59,892 KB
testcase_45 AC 489 ms
60,044 KB
testcase_46 AC 603 ms
60,092 KB
testcase_47 AC 940 ms
76,360 KB
testcase_48 AC 836 ms
73,832 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