結果

問題 No.1473 おでぶなおばけさん
ユーザー tenten
提出日時 2024-07-29 11:28:05
言語 Java
(openjdk 23)
結果
AC  
実行時間 830 ms / 2,000 ms
コード長 3,830 bytes
コンパイル時間 3,300 ms
コンパイル使用メモリ 89,716 KB
実行使用メモリ 66,700 KB
最終ジャッジ日時 2024-07-29 11:28:39
合計ジャッジ時間 32,731 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

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();
        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);
        UnionFindTree uft = new UnionFindTree(n);
        int ans = 0;
        for (int i = m - 1; i >= 0; i--) {
            uft.unite(roads[i]);
            if (uft.same(0, n - 1)) {
                ans = roads[i].value;
                break;
            }
        }
        List<List<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            if (roads[i].value >= ans) {
                graph.get(roads[i].left).add(roads[i].right);
                graph.get(roads[i].right).add(roads[i].left);
            }
        }
        Deque<Path> deq = new ArrayDeque<>();
        deq.add(new Path(0, 0));
        int[] costs = new int[n];
        Arrays.fill(costs, Integer.MAX_VALUE);
        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));
            }
        }
        System.out.println(ans + " " + costs[n - 1]);
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int size) {
            parents = IntStream.range(0, size).toArray();
        }
        
        public int find(int x) {
            return x == parents[x] ? x : (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 value;
        
        public Road(int left, int right, int value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }
        
        public int compareTo(Road another) {
            return value - another.value;
        }
    }
    
    static class Path {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.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