結果

問題 No.1473 おでぶなおばけさん
ユーザー tentententen
提出日時 2021-04-13 15:36:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,157 ms / 2,000 ms
コード長 4,112 bytes
コンパイル時間 2,797 ms
コンパイル使用メモリ 90,520 KB
実行使用メモリ 104,072 KB
最終ジャッジ日時 2023-09-12 07:31:22
合計ジャッジ時間 43,134 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
50,780 KB
testcase_01 AC 79 ms
52,556 KB
testcase_02 AC 1,066 ms
98,808 KB
testcase_03 AC 799 ms
91,524 KB
testcase_04 AC 717 ms
80,728 KB
testcase_05 AC 379 ms
64,812 KB
testcase_06 AC 979 ms
97,844 KB
testcase_07 AC 1,045 ms
102,260 KB
testcase_08 AC 1,157 ms
104,072 KB
testcase_09 AC 1,003 ms
101,368 KB
testcase_10 AC 658 ms
76,116 KB
testcase_11 AC 600 ms
70,444 KB
testcase_12 AC 678 ms
78,580 KB
testcase_13 AC 361 ms
60,216 KB
testcase_14 AC 318 ms
57,632 KB
testcase_15 AC 414 ms
61,844 KB
testcase_16 AC 584 ms
78,632 KB
testcase_17 AC 201 ms
59,728 KB
testcase_18 AC 237 ms
59,628 KB
testcase_19 AC 660 ms
77,528 KB
testcase_20 AC 821 ms
91,352 KB
testcase_21 AC 868 ms
88,620 KB
testcase_22 AC 961 ms
94,348 KB
testcase_23 AC 824 ms
86,092 KB
testcase_24 AC 834 ms
82,744 KB
testcase_25 AC 969 ms
95,044 KB
testcase_26 AC 985 ms
94,484 KB
testcase_27 AC 549 ms
68,368 KB
testcase_28 AC 1,048 ms
102,816 KB
testcase_29 AC 740 ms
82,020 KB
testcase_30 AC 824 ms
93,524 KB
testcase_31 AC 1,127 ms
100,460 KB
testcase_32 AC 880 ms
95,312 KB
testcase_33 AC 831 ms
83,504 KB
testcase_34 AC 602 ms
76,168 KB
testcase_35 AC 588 ms
73,628 KB
testcase_36 AC 825 ms
80,868 KB
testcase_37 AC 847 ms
87,380 KB
testcase_38 AC 333 ms
59,708 KB
testcase_39 AC 594 ms
78,948 KB
testcase_40 AC 594 ms
81,652 KB
testcase_41 AC 409 ms
62,852 KB
testcase_42 AC 410 ms
62,808 KB
testcase_43 AC 729 ms
90,372 KB
testcase_44 AC 736 ms
90,148 KB
testcase_45 AC 744 ms
89,996 KB
testcase_46 AC 785 ms
82,716 KB
testcase_47 AC 868 ms
90,828 KB
testcase_48 AC 896 ms
94,788 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