結果

問題 No.1473 おでぶなおばけさん
ユーザー tenten
提出日時 2024-02-08 13:09:46
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 3,776 bytes
コンパイル時間 2,478 ms
コンパイル使用メモリ 84,780 KB
実行使用メモリ 81,936 KB
最終ジャッジ日時 2024-09-28 12:44:07
合計ジャッジ時間 33,010 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        UnionFindTree uft = new UnionFindTree(n);
        Road weight = IntStream.range(0, m).mapToObj(i -> new Road(sc.nextInt() - 1, sc.nextInt() - 1, sc.nextInt()))
            .sorted()
            .filter(x -> uft.unite(x)).findFirst().orElse(null);
        System.out.println(weight.value + " " + uft.getCost());
    }
    
    static class UnionFindTree {
        int size;
        int[] parents;
        List<List<Integer>> graph;
        
        public UnionFindTree(int size) {
            this.size = size;
            parents = IntStream.range(0, size).map(i -> i).toArray();
            graph = IntStream.range(0,size).mapToObj(i -> new ArrayList<Integer>()).collect(Collectors.toList());
        }
        
        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 unite(Road r) {
            if (!same(0, size - 1)) {
                int left = find(r.left);
                int right = find(r.right);
                if (left != right) {
                    parents[left] = right;
                }
                graph.get(r.left).add(r.right);
                graph.get(r.right).add(r.left);
            }
            return same(0, size - 1);
        }
        
        public int getCost() {
            int[] costs = new int[size];
            Arrays.fill(costs, Integer.MAX_VALUE);
            ArrayDeque<Path> deq = new ArrayDeque<>();
            deq.add(new Path(0, 0));
            while (deq.size() > 0) {
                Path p = deq.poll();
                if (costs[p.idx] <= p.value) {
                    continue;
                }
                costs[p.idx] = p.value;
                for (int x : graph.get(p.idx)) {
                    deq.add(new Path(x, p.value + 1));
                }
            }
            return costs[size - 1];
        }
    }
    
    static class Path {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
    }
    
    static class Road implements Comparable<Road> {
        int left;
        int right;
        int value;
        
        public Road(int left, int right, int value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }
        
        public int compareTo(Road another) {
            return another.value - value;
        }
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0