結果

問題 No.1382 Travel in Mitaru city
ユーザー tentententen
提出日時 2021-02-08 11:26:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 796 ms / 2,000 ms
コード長 1,997 bytes
コンパイル時間 3,823 ms
コンパイル使用メモリ 75,728 KB
実行使用メモリ 82,908 KB
最終ジャッジ日時 2023-09-18 17:46:01
合計ジャッジ時間 35,994 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,616 KB
testcase_01 AC 44 ms
49,920 KB
testcase_02 AC 44 ms
49,708 KB
testcase_03 AC 43 ms
49,776 KB
testcase_04 AC 43 ms
49,556 KB
testcase_05 AC 44 ms
49,600 KB
testcase_06 AC 416 ms
62,148 KB
testcase_07 AC 313 ms
61,880 KB
testcase_08 AC 241 ms
60,624 KB
testcase_09 AC 392 ms
64,920 KB
testcase_10 AC 408 ms
62,452 KB
testcase_11 AC 552 ms
67,412 KB
testcase_12 AC 562 ms
67,588 KB
testcase_13 AC 577 ms
67,448 KB
testcase_14 AC 474 ms
67,352 KB
testcase_15 AC 523 ms
67,232 KB
testcase_16 AC 767 ms
80,860 KB
testcase_17 AC 752 ms
81,040 KB
testcase_18 AC 737 ms
81,360 KB
testcase_19 AC 750 ms
80,364 KB
testcase_20 AC 753 ms
80,820 KB
testcase_21 AC 796 ms
82,840 KB
testcase_22 AC 777 ms
81,416 KB
testcase_23 AC 746 ms
80,936 KB
testcase_24 AC 766 ms
80,796 KB
testcase_25 AC 708 ms
81,588 KB
testcase_26 AC 700 ms
80,848 KB
testcase_27 AC 696 ms
79,448 KB
testcase_28 AC 691 ms
80,628 KB
testcase_29 AC 685 ms
81,796 KB
testcase_30 AC 694 ms
79,804 KB
testcase_31 AC 553 ms
73,332 KB
testcase_32 AC 670 ms
82,908 KB
testcase_33 AC 616 ms
79,940 KB
testcase_34 AC 454 ms
66,964 KB
testcase_35 AC 344 ms
64,244 KB
testcase_36 AC 465 ms
74,160 KB
testcase_37 AC 187 ms
58,444 KB
testcase_38 AC 509 ms
79,484 KB
testcase_39 AC 242 ms
61,176 KB
testcase_40 AC 416 ms
66,952 KB
testcase_41 AC 441 ms
73,444 KB
testcase_42 AC 506 ms
80,620 KB
testcase_43 AC 452 ms
71,404 KB
testcase_44 AC 266 ms
62,900 KB
testcase_45 AC 426 ms
70,996 KB
testcase_46 AC 292 ms
60,748 KB
testcase_47 AC 730 ms
80,372 KB
testcase_48 AC 529 ms
66,936 KB
testcase_49 AC 720 ms
82,000 KB
testcase_50 AC 401 ms
64,652 KB
testcase_51 AC 561 ms
74,004 KB
testcase_52 AC 572 ms
80,608 KB
testcase_53 AC 240 ms
60,524 KB
testcase_54 AC 352 ms
64,804 KB
testcase_55 AC 584 ms
80,612 KB
testcase_56 AC 306 ms
61,012 KB
testcase_57 AC 436 ms
67,860 KB
testcase_58 AC 160 ms
55,764 KB
testcase_59 AC 299 ms
62,012 KB
testcase_60 AC 595 ms
75,808 KB
testcase_61 AC 111 ms
52,848 KB
testcase_62 AC 45 ms
49,544 KB
testcase_63 AC 190 ms
57,464 KB
testcase_64 AC 132 ms
55,356 KB
testcase_65 AC 64 ms
50,764 KB
testcase_66 AC 118 ms
52,720 KB
testcase_67 AC 124 ms
55,008 KB
testcase_68 AC 140 ms
55,576 KB
testcase_69 AC 120 ms
52,972 KB
testcase_70 AC 148 ms
55,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 4);
        int n = Integer.parseInt(first[0]);
        int m = Integer.parseInt(first[1]);
        int s = Integer.parseInt(first[2]) - 1;
        int t = Integer.parseInt(first[3]) - 1;
        String[] second = br.readLine().split(" ", n);
        Town[] towns = new Town[n];
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            towns[i] = new Town(i, Integer.parseInt(second[i]));
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            String[] line = br.readLine().split(" ", 2);
            int a = Integer.parseInt(line[0]) - 1;
            int b = Integer.parseInt(line[1]) - 1;
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
        PriorityQueue<Town> queue = new PriorityQueue<>();
        queue.add(towns[s]);
        boolean[] visited = new boolean[n];
        visited[s] = true;
        int score = Integer.MAX_VALUE;
        int count = 0;
        while (queue.size() > 0) {
            Town y = queue.poll();
            if (score > y.value) {
                score = y.value;
                count++;
            }
            for (int x : graph.get(y.idx)) {
                if (!visited[x]) {
                    queue.add(towns[x]);
                    visited[x] = true;
                }
            }
        }
        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