Submission #1779104


Source Code Expand

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

public class Main {
    private static IO io = new IO();
    private static final int INF = 1145141919;
    private static int n = io.nextInt();

    public static void main(String[] args) {
        int m = io.nextInt();
        List<ArrayList<int[]>> list = new ArrayList<ArrayList<int[]>>();
        for (int i=0; i<n; i++) list.add(new ArrayList<int[]>());
        for (int i=0; i<m; i++) {
            int a = io.nextInt()-1;
            int b = io.nextInt()-1;
            int c = io.nextInt();
            list.get(a).add(new int[] {b, c});
            list.get(b).add(new int[] {a, c});
        }

        // 各頂点を始点として全ての点への最短距離を求め、"一番遠い点への距離"の最小値を求める
        int ans = INF;
        for (int i = 0; i < n; i++) ans = Math.min(ans, dijkstra(i, list));
        System.out.println(ans);
    }

    private static int dijkstra(int s, List<ArrayList<int[]>> list) {
        int dis[] = new int[n];	// 始点からの最短距離
        int ans = 0;    // 一番遠い点までの距離
        Arrays.fill(dis, INF);	// 各頂点までの距離を初期化
        dis[s] = 0;	// 始点の距離は0
        PriorityQueue<int[]> que = new PriorityQueue<int[]>(n, new Comparator<int[]>() {
            public int compare (int a[], int b[]) {
                return a[0]-b[0];	// 第1キーで昇順ソート
            }
        });
        que.add(new int[] {0, s});

        while (!que.isEmpty()) {
            int poll[] = que.poll();
            int total = poll[0];    // 移動してきた距離
            int now = poll[1];    // いまいる頂点
            if (dis[now]<total) continue;
            int way = list.get(now).size(); // いまいる頂点から出てる辺の数
            for (int i=0; i<way; i++) {
                int go = list.get(now).get(i)[0];
                int cost = list.get(now).get(i)[1];
                if (dis[now]+cost<dis[go]) {
                    dis[go] = dis[now] + cost;
                    ans = Math.max(ans, dis[go]);
                    que.offer(new int[] {dis[go], go});
                }
            }
        }

        return ans;
    }


    static class IO extends PrintWriter {
        private final InputStream in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;

        IO() {
            this(System.in);
        }

        IO(InputStream source) {
            super(System.out);
            this.in = source;
        }

        boolean hasNextByte() {
            if (ptr < buflen) return true;
            else {
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (buflen <= 0) return false;
            }
            return true;
        }
        int readByte() {
            if (hasNextByte()) return buffer[ptr++];
            else return -1;
        }
        boolean isPrintableChar(int c) {return 33<=c &&c <=126;}
        void skipUnprintable() {while(hasNextByte() && !isPrintableChar(buffer[ptr]))ptr++;}
        boolean hasNext() {
            skipUnprintable();
            return hasNextByte();
        }

        long nextLong() {
            if (!hasNext()) throw new NoSuchElementException();
            long n = 0;
            boolean minus = false;
            int b = readByte();
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            if (b < '0' || '9' < b) throw new NumberFormatException();
            while (true) {
                if ('0' <= b && b <= '9') {
                    n *= 10;
                    n += b - '0';
                } else if (b == -1 || !isPrintableChar(b)) return minus ? -n : n;
                else throw new NumberFormatException();
                b = readByte();
            }
        }

        int nextInt() {
            long nl = nextLong();
            if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
            return (int) nl;
        }

        public void close() {
            super.close();
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }
}

Submission Info

Submission Time
Task D - バスと避けられない運命
User creep04
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 4538 Byte
Status WA
Exec Time 535 ms
Memory 47072 KB

Judge Result

Set Name All
Score / Max Score 0 / 100
Status
AC × 18
WA × 21
Set Name Test Cases
All sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt
Case Name Status Exec Time Memory
sample_01.txt AC 72 ms 20820 KB
sample_02.txt AC 77 ms 21076 KB
sample_03.txt AC 82 ms 20820 KB
test_01.txt AC 71 ms 21332 KB
test_02.txt AC 144 ms 23528 KB
test_03.txt WA 535 ms 45056 KB
test_04.txt WA 175 ms 25436 KB
test_05.txt WA 313 ms 29448 KB
test_06.txt WA 169 ms 25852 KB
test_07.txt WA 467 ms 47072 KB
test_08.txt WA 84 ms 19668 KB
test_09.txt WA 285 ms 28500 KB
test_10.txt WA 158 ms 21224 KB
test_11.txt WA 175 ms 24884 KB
test_12.txt WA 274 ms 32212 KB
test_13.txt WA 180 ms 28236 KB
test_14.txt WA 221 ms 25876 KB
test_15.txt WA 464 ms 37428 KB
test_16.txt WA 223 ms 25012 KB
test_17.txt WA 292 ms 28168 KB
test_18.txt WA 327 ms 29736 KB
test_19.txt WA 240 ms 26700 KB
test_20.txt WA 205 ms 26424 KB
test_21.txt WA 74 ms 20948 KB
test_22.txt WA 242 ms 27476 KB
test_23.txt WA 78 ms 19412 KB
test_24.txt AC 128 ms 27052 KB
test_25.txt AC 131 ms 25116 KB
test_26.txt AC 99 ms 21588 KB
test_27.txt AC 115 ms 21332 KB
test_28.txt AC 122 ms 23088 KB
test_29.txt AC 128 ms 23208 KB
test_30.txt AC 96 ms 21716 KB
test_31.txt AC 78 ms 20948 KB
test_32.txt AC 126 ms 24880 KB
test_33.txt AC 127 ms 21412 KB
test_34.txt AC 81 ms 20436 KB
test_35.txt AC 99 ms 21588 KB
test_36.txt AC 128 ms 24916 KB