結果

問題 No.1473 おでぶなおばけさん
ユーザー tentententen
提出日時 2022-08-05 09:28:18
言語 Java21
(openjdk 21)
結果
AC  
実行時間 744 ms / 2,000 ms
コード長 3,882 bytes
コンパイル時間 2,429 ms
コンパイル使用メモリ 81,396 KB
実行使用メモリ 78,768 KB
最終ジャッジ日時 2023-10-13 07:54:04
合計ジャッジ時間 30,056 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
49,096 KB
testcase_01 AC 65 ms
51,140 KB
testcase_02 AC 723 ms
78,440 KB
testcase_03 AC 523 ms
68,720 KB
testcase_04 AC 500 ms
66,144 KB
testcase_05 AC 302 ms
59,528 KB
testcase_06 AC 607 ms
68,916 KB
testcase_07 AC 676 ms
78,028 KB
testcase_08 AC 744 ms
77,868 KB
testcase_09 AC 678 ms
78,360 KB
testcase_10 AC 424 ms
62,500 KB
testcase_11 AC 410 ms
62,424 KB
testcase_12 AC 416 ms
62,460 KB
testcase_13 AC 344 ms
60,548 KB
testcase_14 AC 300 ms
59,696 KB
testcase_15 AC 399 ms
62,048 KB
testcase_16 AC 425 ms
62,124 KB
testcase_17 AC 175 ms
59,276 KB
testcase_18 AC 201 ms
59,812 KB
testcase_19 AC 406 ms
62,252 KB
testcase_20 AC 484 ms
66,568 KB
testcase_21 AC 533 ms
64,192 KB
testcase_22 AC 565 ms
74,584 KB
testcase_23 AC 478 ms
73,280 KB
testcase_24 AC 498 ms
69,272 KB
testcase_25 AC 584 ms
75,208 KB
testcase_26 AC 577 ms
73,884 KB
testcase_27 AC 363 ms
62,076 KB
testcase_28 AC 647 ms
73,168 KB
testcase_29 AC 440 ms
64,364 KB
testcase_30 AC 478 ms
66,428 KB
testcase_31 AC 583 ms
78,768 KB
testcase_32 AC 484 ms
68,324 KB
testcase_33 AC 420 ms
66,548 KB
testcase_34 AC 350 ms
63,312 KB
testcase_35 AC 332 ms
59,508 KB
testcase_36 AC 451 ms
66,348 KB
testcase_37 AC 481 ms
68,596 KB
testcase_38 AC 255 ms
60,164 KB
testcase_39 AC 425 ms
62,256 KB
testcase_40 AC 425 ms
62,256 KB
testcase_41 AC 427 ms
62,860 KB
testcase_42 AC 404 ms
62,128 KB
testcase_43 AC 460 ms
69,904 KB
testcase_44 AC 477 ms
69,940 KB
testcase_45 AC 506 ms
69,896 KB
testcase_46 AC 584 ms
71,868 KB
testcase_47 AC 629 ms
71,340 KB
testcase_48 AC 567 ms
69,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        Bridge[] bridges = new Bridge[m];
        for (int i = 0; i < m; i++) {
            bridges[i] = new Bridge(sc.nextInt() - 1, sc.nextInt() - 1, sc.nextInt());
        }
        Arrays.sort(bridges);
        UnionFindTree uft = new UnionFindTree(n);
        int max = 0;
        for (Bridge x : bridges) {
            uft.unite(x);
            if (uft.same(0, n - 1)) {
                max = x.value;
                break;
            }
        }
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (Bridge x : bridges) {
            if (x.value >= max) {
                graph.get(x.left).add(x.right);
                graph.get(x.right).add(x.left);
            }
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, 0));
        int[] costs = new int[n];
        Arrays.fill(costs, Integer.MAX_VALUE);
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs[p.idx] <= p.value) {
                continue;
            }
            costs[p.idx] = p.value;
            for (int x : graph.get(p.idx)) {
                queue.add(new Path(x, p.value + 1));
            }
        }
        System.out.println(max + " " + costs[n - 1]);
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }
        
        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 same(Bridge x) {
            return same(x.left, x.right);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                parents[find(x)] = find(y);
            }
        }
        
        public void unite(Bridge b) {
            unite(b.left, b.right);
        }
    }
    
    static class Bridge implements Comparable<Bridge> {
        int left;
        int right;
        int value;
        
        public Bridge(int left, int right, int value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }
        
        public int compareTo(Bridge another) {
            return another.value - value;
        }
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0