結果

問題 No.1382 Travel in Mitaru city
ユーザー tenten
提出日時 2021-02-08 11:06:27
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,645 bytes
コンパイル時間 2,356 ms
コンパイル使用メモリ 79,248 KB
実行使用メモリ 198,916 KB
最終ジャッジ日時 2024-07-05 07:21:47
合計ジャッジ時間 8,746 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 3 TLE * 1 -- * 64
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int s = sc.nextInt() - 1;
        int t = sc.nextInt() - 1;
        Town[] towns = new Town[n];
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            towns[i] = new Town(i, sc.nextInt());
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
        PriorityQueue<Town> queue = new PriorityQueue<>();
        queue.add(towns[s]);
        HashSet<Integer> visited = new HashSet<>();
        int score = Integer.MAX_VALUE;
        int count = 0;
        while (queue.size() > 0) {
            Town y = queue.poll();
            if (score > y.value) {
                score = y.value;
                count++;
            }
            visited.add(y.idx);
            for (int x : graph.get(y.idx)) {
                if (!visited.contains(x)) {
                    queue.add(towns[x]);
                }
            }
        }
        System.out.println(count - 1);
    }
    
    static class Town implements Comparable<Town> {
        int idx;
        int value;
        
        public Town(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Town another) {
            return another.value - value;
        }
    }
}
0