結果

問題 No.1473 おでぶなおばけさん
ユーザー tentententen
提出日時 2022-09-30 13:21:12
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,714 bytes
コンパイル時間 2,410 ms
コンパイル使用メモリ 81,292 KB
実行使用メモリ 79,400 KB
最終ジャッジ日時 2024-06-02 02:25:32
合計ジャッジ時間 26,895 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
51,180 KB
testcase_01 AC 74 ms
51,516 KB
testcase_02 AC 663 ms
76,880 KB
testcase_03 AC 521 ms
68,216 KB
testcase_04 AC 452 ms
65,932 KB
testcase_05 AC 293 ms
59,336 KB
testcase_06 AC 593 ms
68,912 KB
testcase_07 AC 654 ms
77,048 KB
testcase_08 AC 688 ms
79,112 KB
testcase_09 AC 642 ms
79,400 KB
testcase_10 AC 423 ms
62,172 KB
testcase_11 AC 445 ms
61,764 KB
testcase_12 AC 431 ms
62,120 KB
testcase_13 AC 350 ms
59,272 KB
testcase_14 AC 296 ms
59,368 KB
testcase_15 AC 401 ms
61,656 KB
testcase_16 AC 423 ms
61,704 KB
testcase_17 AC 181 ms
58,172 KB
testcase_18 AC 201 ms
59,304 KB
testcase_19 AC 422 ms
62,296 KB
testcase_20 AC 465 ms
66,744 KB
testcase_21 AC 526 ms
64,168 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 544 ms
73,208 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 563 ms
78,428 KB
testcase_32 AC 434 ms
68,004 KB
testcase_33 AC 409 ms
66,904 KB
testcase_34 AC 346 ms
63,460 KB
testcase_35 AC 317 ms
60,996 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 429 ms
61,568 KB
testcase_40 AC 403 ms
62,280 KB
testcase_41 AC 406 ms
61,408 KB
testcase_42 AC 419 ms
61,660 KB
testcase_43 AC 464 ms
68,156 KB
testcase_44 AC 470 ms
68,224 KB
testcase_45 AC 468 ms
68,472 KB
testcase_46 AC 549 ms
68,728 KB
testcase_47 AC 607 ms
74,076 KB
testcase_48 AC 552 ms
70,140 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();
        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);
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        UnionFindTree uft = new UnionFindTree(n);
        int ans = 0;
        for (Road x : roads) {
            uft.unite(x);
            graph.get(x.left).add(x.right);
            graph.get(x.right).add(x.left);
            if (uft.same(0, n - 1)) {
                ans = x.weight;
                break;
            }
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        int[] costs = new int[n];
        Arrays.fill(costs, Integer.MAX_VALUE);
        queue.add(new Path(0, 0));
        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(ans + " " + costs[n - 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;
        }
    }
    
    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 another) {
            return another.weight - weight;
        }
    }
    
    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 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);
        }
    }
}
    
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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