結果

問題 No.1473 おでぶなおばけさん
ユーザー tenten
提出日時 2022-09-30 13:21:12
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 3,714 bytes
コンパイル時間 3,730 ms
コンパイル使用メモリ 89,860 KB
実行使用メモリ 66,612 KB
最終ジャッジ日時 2024-12-22 18:45:44
合計ジャッジ時間 33,217 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36 WA * 11
権限があれば一括ダウンロードができます

ソースコード

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