結果

問題 No.1473 おでぶなおばけさん
ユーザー shojin_proshojin_pro
提出日時 2021-04-10 01:09:26
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,093 ms / 2,000 ms
コード長 8,160 bytes
コンパイル時間 3,453 ms
コンパイル使用メモリ 84,156 KB
実行使用メモリ 107,044 KB
最終ジャッジ日時 2023-09-07 20:09:05
合計ジャッジ時間 35,932 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
53,036 KB
testcase_01 AC 85 ms
53,464 KB
testcase_02 AC 983 ms
97,020 KB
testcase_03 AC 750 ms
82,108 KB
testcase_04 AC 599 ms
78,784 KB
testcase_05 AC 368 ms
64,760 KB
testcase_06 AC 848 ms
84,080 KB
testcase_07 AC 1,014 ms
94,288 KB
testcase_08 AC 1,093 ms
105,532 KB
testcase_09 AC 1,066 ms
107,044 KB
testcase_10 AC 550 ms
62,196 KB
testcase_11 AC 549 ms
63,596 KB
testcase_12 AC 537 ms
62,512 KB
testcase_13 AC 424 ms
63,096 KB
testcase_14 AC 380 ms
59,868 KB
testcase_15 AC 504 ms
63,156 KB
testcase_16 AC 543 ms
63,480 KB
testcase_17 AC 227 ms
57,960 KB
testcase_18 AC 259 ms
60,696 KB
testcase_19 AC 503 ms
66,560 KB
testcase_20 AC 730 ms
79,984 KB
testcase_21 AC 670 ms
69,864 KB
testcase_22 AC 805 ms
94,716 KB
testcase_23 AC 788 ms
84,092 KB
testcase_24 AC 738 ms
86,940 KB
testcase_25 AC 772 ms
83,628 KB
testcase_26 AC 725 ms
83,212 KB
testcase_27 AC 399 ms
65,620 KB
testcase_28 AC 919 ms
96,992 KB
testcase_29 AC 580 ms
69,868 KB
testcase_30 AC 615 ms
77,460 KB
testcase_31 AC 871 ms
97,408 KB
testcase_32 AC 671 ms
80,072 KB
testcase_33 AC 601 ms
77,896 KB
testcase_34 AC 401 ms
68,240 KB
testcase_35 AC 399 ms
63,208 KB
testcase_36 AC 649 ms
71,860 KB
testcase_37 AC 677 ms
82,484 KB
testcase_38 AC 287 ms
62,136 KB
testcase_39 AC 524 ms
61,876 KB
testcase_40 AC 542 ms
62,388 KB
testcase_41 AC 525 ms
61,376 KB
testcase_42 AC 531 ms
62,272 KB
testcase_43 AC 540 ms
78,740 KB
testcase_44 AC 541 ms
80,032 KB
testcase_45 AC 529 ms
78,456 KB
testcase_46 AC 755 ms
82,260 KB
testcase_47 AC 889 ms
91,584 KB
testcase_48 AC 849 ms
92,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static FastScanner sc = new FastScanner(System.in);
    static PrintWriter pw = new PrintWriter(System.out);
    static StringBuilder sb = new StringBuilder();

    static ArrayList<ArrayList<Edge>> G = new ArrayList<>();
    static ArrayList<Edge> arr = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        solve();
        pw.flush();
    }

    public static void solve() {
        int N = sc.nextInt();
        int E = sc.nextInt();
        DSU dsu = new DSU(N);
        for(int i = 0; i < N; i++){
            G.add(new ArrayList<>());
        }
        for (int i = 0; i < E; i++) {
            int a = sc.nextInt()-1;
            int b = sc.nextInt()-1;
            long d = sc.nextLong();
            arr.add(new Edge(a,b,d));
        }
        Collections.sort(arr);
        long ans = 0L;
        for(int i = 0; i < E; i++){
            Edge e = arr.get(i);
            if(ans > e.max){
                break;
            }
            //pw.println(e.from +" " + e.to + " " + e.max);
            dsu.merge(e.from,e.to);
            G.get(e.from).add(new Edge(e.from,e.to,e.max));
            G.get(e.to).add(new Edge(e.to,e.from,e.max));
            if(dsu.same(0,N-1)){
                ans = e.max;
            }
        }
        PriorityQueue<Point> pq = new PriorityQueue<>();
        pq.add(new Point(0,0));
        int[] min = new int[N];
        Arrays.fill(min,(int)1e10);
        min[0] = 0;
        while(pq.size() > 0){
            Point p = pq.poll();
            if(p.now == N-1){
                pw.println(ans + " " + p.cnt);
                return;
            }
            if(min[p.now] < p.cnt){
                continue;
            }
            for(Edge e : G.get(p.now)){
                if(min[e.to] > p.cnt+1){
                    min[e.to] = p.cnt+1;
                    pq.add(new Point(e.to,p.cnt+1));
                }
            }
        }
    }

    static class GeekInteger {
        public static void save_sort(int[] array) {
            shuffle(array);
            Arrays.sort(array);
        }

        public static int[] shuffle(int[] array) {
            int n = array.length;
            Random random = new Random();
            for (int i = 0, j; i < n; i++) {
                j = i + random.nextInt(n - i);
                int randomElement = array[j];
                array[j] = array[i];
                array[i] = randomElement;
            }
            return array;
        }

        public static void save_sort(long[] array) {
            shuffle(array);
            Arrays.sort(array);
        }

        public static long[] shuffle(long[] array) {
            int n = array.length;
            Random random = new Random();
            for (int i = 0, j; i < n; i++) {
                j = i + random.nextInt(n - i);
                long randomElement = array[j];
                array[j] = array[i];
                array[i] = randomElement;
            }
            return array;
        }
    }
    static class Point implements Comparable<Point>{
        int now, cnt;
        public Point(int now, int cnt) {
            this.now = now;
            this.cnt = cnt;
        }
        
        public int compareTo(Point p){
            if(this.cnt < p.cnt){
                return -1;
            }else if(this.cnt > p.cnt){
                return 1;
            }else{
                return 0;
            }
        }
    }
    
    static class Edge implements Comparable<Edge>{
        int from, to;
        long max;
        public Edge(int from, int to, long max) {
            this.from = from;
            this.to = to;
            this.max = max;
        }
        
        public int compareTo(Edge e){
            if(this.max > e.max){
                return -1;
            }else if(this.max < e.max){
                return 1;
            }else{
                return 0;
            }
        }
    }
}

class DSU {
    private int n;
    private int[] parentOrSize;
    private java.util.ArrayList<java.util.ArrayList<Integer>> map;

    public DSU(int n) {
        this.n = n;
        this.map = new java.util.ArrayList<java.util.ArrayList<Integer>>();
        for (int i = 0; i < n; i++) {
            this.map.add(new java.util.ArrayList<Integer>());
            this.map.get(i).add(i);
        }
        this.parentOrSize = new int[n];
        java.util.Arrays.fill(parentOrSize, -1);
    }

    int merge(int a, int b) {
        if (!(0 <= a && a < n))
            throw new IndexOutOfBoundsException("a=" + a);
        if (!(0 <= b && b < n))
            throw new IndexOutOfBoundsException("b=" + b);

        int x = leader(a);
        int y = leader(b);
        if (x == y)
            return x;
        if (-parentOrSize[x] < -parentOrSize[y]) {
            int tmp = x;
            x = y;
            y = tmp;
        }
        parentOrSize[x] += parentOrSize[y];
        parentOrSize[y] = x;
        this.map.get(x).addAll(this.map.get(y));
        return x;
    }

    boolean same(int a, int b) {
        if (!(0 <= a && a < n))
            throw new IndexOutOfBoundsException("a=" + a);
        if (!(0 <= b && b < n))
            throw new IndexOutOfBoundsException("b=" + b);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        if (parentOrSize[a] < 0) {
            return a;
        } else {
            parentOrSize[a] = leader(parentOrSize[a]);
            return parentOrSize[a];
        }
    }

    int size(int a) {
        if (!(0 <= a && a < n))
            throw new IndexOutOfBoundsException("" + a);
        return -parentOrSize[leader(a)];
    }

    java.util.ArrayList<java.util.ArrayList<Integer>> groups() {
        int[] leaderBuf = new int[n];
        int[] groupSize = new int[n];
        for (int i = 0; i < n; i++) {
            leaderBuf[i] = leader(i);
            groupSize[leaderBuf[i]]++;
        }
        java.util.ArrayList<java.util.ArrayList<Integer>> result = new java.util.ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            result.add(new java.util.ArrayList<>(groupSize[i]));
        }
        for (int i = 0; i < n; i++) {
            result.get(leaderBuf[i]).add(i);
        }
        result.removeIf(java.util.ArrayList::isEmpty);
        return result;
    }

    java.util.ArrayList<Integer> getArray(int n) {
        return this.map.get(n);
    }
}

class FastScanner {
    private BufferedReader reader = null;
    private StringTokenizer tokenizer = null;

    public FastScanner(InputStream in) {
        reader = new BufferedReader(new InputStreamReader(in));
        tokenizer = null;
    }

    public FastScanner(FileReader in) {
        reader = new BufferedReader(in);
        tokenizer = null;
    }

    public String next() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public String nextLine() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                return reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken("\n");
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }

    public String[] nextArray(int n) {
        String[] a = new String[n];
        for (int i = 0; i < n; i++)
            a[i] = next();
        return a;
    }

    public int[] nextIntArray(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    public long[] nextLongArray(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    }
}
0