結果

問題 No.1473 おでぶなおばけさん
ユーザー shojin_proshojin_pro
提出日時 2021-04-10 00:52:15
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 8,016 bytes
コンパイル時間 2,733 ms
コンパイル使用メモリ 81,948 KB
実行使用メモリ 86,920 KB
最終ジャッジ日時 2023-09-07 19:48:57
合計ジャッジ時間 30,767 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
53,048 KB
testcase_01 AC 83 ms
53,076 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 421 ms
64,044 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 507 ms
62,124 KB
testcase_42 AC 492 ms
62,280 KB
testcase_43 AC 482 ms
78,036 KB
testcase_44 AC 474 ms
75,876 KB
testcase_45 AC 485 ms
76,648 KB
testcase_46 TLE -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

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 = (long)1e10;
        for(int i = 0; i < E; i++){
            Edge e = arr.get(i);
            ans = e.max;
            dsu.merge(e.from,e.to);
            G.get(e.from).add(e);
            G.get(e.to).add(e);
            if(dsu.same(0,N-1)){
                ans = e.max;
                break;
            }
        }
        PriorityQueue<Point> pq = new PriorityQueue<>();
        pq.add(new Point(0,0));
        int[] min = new int[N];
        Arrays.fill(min,(int)1e9);
        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;
            }
            min[p.now] = p.cnt;
            for(Edge e : G.get(p.now)){
                if(min[e.to] <= p.cnt+1) continue;
                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