結果

問題 No.1473 おでぶなおばけさん
ユーザー tentententen
提出日時 2024-07-29 11:28:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 830 ms / 2,000 ms
コード長 3,830 bytes
コンパイル時間 3,300 ms
コンパイル使用メモリ 89,716 KB
実行使用メモリ 66,700 KB
最終ジャッジ日時 2024-07-29 11:28:39
合計ジャッジ時間 32,731 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
40,268 KB
testcase_01 AC 101 ms
40,268 KB
testcase_02 AC 799 ms
65,312 KB
testcase_03 AC 651 ms
58,968 KB
testcase_04 AC 527 ms
56,076 KB
testcase_05 AC 330 ms
50,164 KB
testcase_06 AC 703 ms
59,464 KB
testcase_07 AC 770 ms
66,428 KB
testcase_08 AC 830 ms
66,360 KB
testcase_09 AC 791 ms
66,312 KB
testcase_10 AC 502 ms
50,968 KB
testcase_11 AC 509 ms
50,876 KB
testcase_12 AC 525 ms
50,812 KB
testcase_13 AC 412 ms
49,744 KB
testcase_14 AC 374 ms
49,512 KB
testcase_15 AC 475 ms
50,828 KB
testcase_16 AC 510 ms
51,024 KB
testcase_17 AC 223 ms
45,924 KB
testcase_18 AC 248 ms
47,848 KB
testcase_19 AC 459 ms
51,796 KB
testcase_20 AC 587 ms
56,968 KB
testcase_21 AC 579 ms
53,764 KB
testcase_22 AC 635 ms
64,148 KB
testcase_23 AC 532 ms
60,460 KB
testcase_24 AC 546 ms
59,072 KB
testcase_25 AC 627 ms
63,164 KB
testcase_26 AC 644 ms
62,152 KB
testcase_27 AC 362 ms
52,248 KB
testcase_28 AC 729 ms
61,852 KB
testcase_29 AC 516 ms
54,388 KB
testcase_30 AC 580 ms
55,236 KB
testcase_31 AC 673 ms
66,700 KB
testcase_32 AC 540 ms
58,320 KB
testcase_33 AC 488 ms
56,720 KB
testcase_34 AC 359 ms
52,068 KB
testcase_35 AC 350 ms
50,484 KB
testcase_36 AC 503 ms
56,728 KB
testcase_37 AC 554 ms
58,408 KB
testcase_38 AC 277 ms
49,516 KB
testcase_39 AC 487 ms
50,568 KB
testcase_40 AC 479 ms
50,716 KB
testcase_41 AC 478 ms
51,112 KB
testcase_42 AC 472 ms
50,964 KB
testcase_43 AC 502 ms
58,988 KB
testcase_44 AC 502 ms
58,900 KB
testcase_45 AC 484 ms
59,076 KB
testcase_46 AC 629 ms
59,232 KB
testcase_47 AC 673 ms
61,296 KB
testcase_48 AC 654 ms
59,700 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();
        Road[] roads = new Road[m];
        for (int i = 0; i < m; i++) {
            roads[i] = new Road(sc.nextInt() - 1, sc.nextInt() - 1, sc.nextInt());
        }
        Arrays.sort(roads);
        UnionFindTree uft = new UnionFindTree(n);
        int ans = 0;
        for (int i = m - 1; i >= 0; i--) {
            uft.unite(roads[i]);
            if (uft.same(0, n - 1)) {
                ans = roads[i].value;
                break;
            }
        }
        List<List<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            if (roads[i].value >= ans) {
                graph.get(roads[i].left).add(roads[i].right);
                graph.get(roads[i].right).add(roads[i].left);
            }
        }
        Deque<Path> deq = new ArrayDeque<>();
        deq.add(new Path(0, 0));
        int[] costs = new int[n];
        Arrays.fill(costs, Integer.MAX_VALUE);
        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));
            }
        }
        System.out.println(ans + " " + costs[n - 1]);
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int size) {
            parents = IntStream.range(0, size).toArray();
        }
        
        public int find(int x) {
            return x == parents[x] ? x : (parents[x] = find(parents[x]));
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public boolean same(Road r) {
            return same(r.left, r.right);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                parents[find(x)] = find(y);
            }
        }
        
        public void unite(Road r) {
            unite(r.left, r.right);
        }
    }
    
    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 value - another.value;
        }
    }
    
    static class Path {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.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