結果

問題 No.1473 おでぶなおばけさん
ユーザー tenten
提出日時 2021-04-13 15:36:04
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,180 ms / 2,000 ms
コード長 4,112 bytes
コンパイル時間 2,513 ms
コンパイル使用メモリ 83,820 KB
実行使用メモリ 92,868 KB
最終ジャッジ日時 2024-06-29 20:27:59
合計ジャッジ時間 40,178 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

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