結果

問題 No.1473 おでぶなおばけさん
ユーザー tentententen
提出日時 2021-04-13 15:36:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,180 ms / 2,000 ms
コード長 4,112 bytes
コンパイル時間 2,513 ms
コンパイル使用メモリ 83,820 KB
実行使用メモリ 92,868 KB
最終ジャッジ日時 2024-06-29 20:27:59
合計ジャッジ時間 40,178 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
38,004 KB
testcase_01 AC 93 ms
39,644 KB
testcase_02 AC 1,063 ms
87,044 KB
testcase_03 AC 795 ms
78,224 KB
testcase_04 AC 687 ms
68,608 KB
testcase_05 AC 370 ms
50,228 KB
testcase_06 AC 899 ms
86,788 KB
testcase_07 AC 1,031 ms
92,868 KB
testcase_08 AC 1,180 ms
92,012 KB
testcase_09 AC 1,056 ms
89,660 KB
testcase_10 AC 648 ms
64,316 KB
testcase_11 AC 572 ms
61,168 KB
testcase_12 AC 645 ms
65,564 KB
testcase_13 AC 351 ms
51,236 KB
testcase_14 AC 303 ms
49,196 KB
testcase_15 AC 386 ms
49,844 KB
testcase_16 AC 515 ms
64,768 KB
testcase_17 AC 203 ms
45,648 KB
testcase_18 AC 231 ms
48,312 KB
testcase_19 AC 675 ms
67,264 KB
testcase_20 AC 774 ms
82,964 KB
testcase_21 AC 913 ms
74,760 KB
testcase_22 AC 919 ms
85,472 KB
testcase_23 AC 818 ms
74,868 KB
testcase_24 AC 802 ms
74,204 KB
testcase_25 AC 1,138 ms
92,248 KB
testcase_26 AC 960 ms
87,864 KB
testcase_27 AC 496 ms
57,344 KB
testcase_28 AC 994 ms
88,984 KB
testcase_29 AC 722 ms
70,272 KB
testcase_30 AC 818 ms
78,764 KB
testcase_31 AC 1,105 ms
92,104 KB
testcase_32 AC 869 ms
88,032 KB
testcase_33 AC 907 ms
73,088 KB
testcase_34 AC 546 ms
62,336 KB
testcase_35 AC 598 ms
63,512 KB
testcase_36 AC 840 ms
70,600 KB
testcase_37 AC 833 ms
74,680 KB
testcase_38 AC 331 ms
48,464 KB
testcase_39 AC 612 ms
68,984 KB
testcase_40 AC 560 ms
69,548 KB
testcase_41 AC 392 ms
51,784 KB
testcase_42 AC 393 ms
49,544 KB
testcase_43 AC 693 ms
82,740 KB
testcase_44 AC 759 ms
81,572 KB
testcase_45 AC 762 ms
81,624 KB
testcase_46 AC 776 ms
71,040 KB
testcase_47 AC 850 ms
82,164 KB
testcase_48 AC 826 ms
84,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int mm = sc.nextInt();
        for (int i = 0; i < n; i++) {
            graph.add(new HashMap<>());
        }
        PriorityQueue<Road> roads = new PriorityQueue<>();
        for (int i = 0; i < mm; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int d = sc.nextInt();
            roads.add(new Road(a, b, d));
            if (!graph.get(a).containsKey(b) || graph.get(a).get(b) < d) {
                graph.get(a).put(b, d);
            }
            if (!graph.get(b).containsKey(a) || graph.get(b).get(a) < d) {
                graph.get(b).put(a, d);
            }
        }
        UnionFindTree uft = new UnionFindTree(n);
        Road r = null;
        while (!uft.same(0, n - 1)) {
            r = roads.poll();
            uft.unite(r);
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        int[] costs = new int[n];
        queue.add(new Path(0, 0));
        check(r.weight, costs, queue);
        System.out.println(r.weight + " " + 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(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 weight;
        
        public Road(int left, int right, int weight) {
            this.left = left;
            this.right = right;
            this.weight = weight;
        }
        
        public int compareTo(Road r) {
            return r.weight - weight;
        }
    }
    
    static void check(int size, int[] costs, PriorityQueue<Path> queue) {
             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).keySet()) {
                    if (graph.get(p.idx).get(x) < size) {
                        continue;
                    }
                    queue.add(new Path(x, p.value + 1));
                }
            }
    }
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0