結果

問題 No.1382 Travel in Mitaru city
ユーザー tentententen
提出日時 2023-03-23 10:02:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 741 ms / 2,000 ms
コード長 3,245 bytes
コンパイル時間 3,114 ms
コンパイル使用メモリ 91,996 KB
実行使用メモリ 68,528 KB
最終ジャッジ日時 2024-09-18 15:20:42
合計ジャッジ時間 38,708 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
36,948 KB
testcase_01 AC 50 ms
36,712 KB
testcase_02 AC 48 ms
36,864 KB
testcase_03 AC 50 ms
37,336 KB
testcase_04 AC 50 ms
36,984 KB
testcase_05 AC 51 ms
37,000 KB
testcase_06 AC 437 ms
51,684 KB
testcase_07 AC 413 ms
49,736 KB
testcase_08 AC 303 ms
48,460 KB
testcase_09 AC 484 ms
52,152 KB
testcase_10 AC 422 ms
50,924 KB
testcase_11 AC 599 ms
57,752 KB
testcase_12 AC 601 ms
57,972 KB
testcase_13 AC 613 ms
58,328 KB
testcase_14 AC 508 ms
57,552 KB
testcase_15 AC 548 ms
56,116 KB
testcase_16 AC 670 ms
64,864 KB
testcase_17 AC 690 ms
64,676 KB
testcase_18 AC 712 ms
64,388 KB
testcase_19 AC 684 ms
64,688 KB
testcase_20 AC 693 ms
64,344 KB
testcase_21 AC 732 ms
64,728 KB
testcase_22 AC 708 ms
64,656 KB
testcase_23 AC 680 ms
64,668 KB
testcase_24 AC 710 ms
64,628 KB
testcase_25 AC 696 ms
64,880 KB
testcase_26 AC 689 ms
64,652 KB
testcase_27 AC 703 ms
64,920 KB
testcase_28 AC 712 ms
64,752 KB
testcase_29 AC 684 ms
65,236 KB
testcase_30 AC 646 ms
64,988 KB
testcase_31 AC 595 ms
63,744 KB
testcase_32 AC 674 ms
65,032 KB
testcase_33 AC 679 ms
65,604 KB
testcase_34 AC 493 ms
60,496 KB
testcase_35 AC 394 ms
54,952 KB
testcase_36 AC 533 ms
64,060 KB
testcase_37 AC 245 ms
47,700 KB
testcase_38 AC 590 ms
66,912 KB
testcase_39 AC 268 ms
49,344 KB
testcase_40 AC 498 ms
64,200 KB
testcase_41 AC 515 ms
63,056 KB
testcase_42 AC 587 ms
65,932 KB
testcase_43 AC 527 ms
63,816 KB
testcase_44 AC 340 ms
52,388 KB
testcase_45 AC 514 ms
62,928 KB
testcase_46 AC 340 ms
52,660 KB
testcase_47 AC 709 ms
65,248 KB
testcase_48 AC 536 ms
62,564 KB
testcase_49 AC 741 ms
68,528 KB
testcase_50 AC 437 ms
55,596 KB
testcase_51 AC 559 ms
62,804 KB
testcase_52 AC 641 ms
66,164 KB
testcase_53 AC 295 ms
51,500 KB
testcase_54 AC 411 ms
54,628 KB
testcase_55 AC 672 ms
65,604 KB
testcase_56 AC 359 ms
52,860 KB
testcase_57 AC 509 ms
59,204 KB
testcase_58 AC 241 ms
48,324 KB
testcase_59 AC 342 ms
52,464 KB
testcase_60 AC 593 ms
63,496 KB
testcase_61 AC 146 ms
44,156 KB
testcase_62 AC 52 ms
37,256 KB
testcase_63 AC 256 ms
47,684 KB
testcase_64 AC 177 ms
45,440 KB
testcase_65 AC 86 ms
38,740 KB
testcase_66 AC 149 ms
41,616 KB
testcase_67 AC 150 ms
43,884 KB
testcase_68 AC 172 ms
44,816 KB
testcase_69 AC 143 ms
41,976 KB
testcase_70 AC 187 ms
44,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
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();
        int s = sc.nextInt() - 1;
        int t = sc.nextInt() - 1;
        City[] cities = new City[n];
        ArrayList<ArrayList<City>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            cities[i] = new City(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(cities[b]);
            graph.get(b).add(cities[a]);
        }
        TreeSet<City> nextTo = new TreeSet<>();
        int current = cities[s].value;
        nextTo.addAll(graph.get(s));
        HashSet<Integer> visited = new HashSet<>();
        visited.add(s);
        int ans = 0;
        while (nextTo.size() > 0) {
            City y = nextTo.pollLast();
            if (visited.contains(y.idx)) {
                continue;
            }
            visited.add(y.idx);
            if (y.value < current) {
                ans++;
                current = y.value;
            }
            nextTo.addAll(graph.get(y.idx));
        }
        System.out.println(ans);
    }
    
    static class City implements Comparable<City> {
        int idx;
        int value;
        
        public City(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(City another) {
            if (value == another.value) {
                return idx - another.idx;
            } else {
                return value - another.value;
            }
        }
        
        public int hashCode() {
            return idx;
        }
        
        public boolean equals(Object o) {
            City x = (City)o;
            return x.idx == idx;
        }
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0