Submission #1779139


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();
        int d[][] = new int[n][n];
        for (int i=0; i<n; i++) Arrays.fill(d[i], INF);
        // 重複した道があるケースもあるのでまず配列で道を厳選してからリストに入れる
        for (int i=0; i<m; i++) {
            int a = io.nextInt()-1;
            int b = io.nextInt()-1;
            int c = io.nextInt();
            d[a][b] = d[b][a] = Math.min(d[a][b], c);
        }

        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 < n; i++) {
            for (int j = 0; j < n; j++) {
                if (d[i][j]!=INF) {
                    list.get(i).add(new int[] {j, d[i][j]});
                }
            }
        }

        // 各頂点を始点として全ての点への最短距離を求め、"一番遠い点への距離"の最小値を求める
        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 4939 Byte
Status WA
Exec Time 596 ms
Memory 45088 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 71 ms 19412 KB
sample_02.txt AC 70 ms 21076 KB
sample_03.txt AC 71 ms 19668 KB
test_01.txt AC 70 ms 18900 KB
test_02.txt AC 146 ms 23020 KB
test_03.txt WA 596 ms 42132 KB
test_04.txt WA 190 ms 28292 KB
test_05.txt WA 294 ms 32172 KB
test_06.txt WA 165 ms 22988 KB
test_07.txt WA 514 ms 40616 KB
test_08.txt WA 84 ms 18772 KB
test_09.txt WA 274 ms 28316 KB
test_10.txt WA 157 ms 25680 KB
test_11.txt WA 164 ms 23000 KB
test_12.txt WA 244 ms 28744 KB
test_13.txt WA 169 ms 26020 KB
test_14.txt WA 202 ms 24548 KB
test_15.txt WA 401 ms 45088 KB
test_16.txt WA 227 ms 23596 KB
test_17.txt WA 321 ms 30568 KB
test_18.txt WA 239 ms 27692 KB
test_19.txt WA 226 ms 24712 KB
test_20.txt WA 203 ms 22984 KB
test_21.txt WA 72 ms 21460 KB
test_22.txt WA 246 ms 30080 KB
test_23.txt WA 78 ms 20948 KB
test_24.txt AC 129 ms 21816 KB
test_25.txt AC 189 ms 26000 KB
test_26.txt AC 107 ms 21972 KB
test_27.txt AC 105 ms 23764 KB
test_28.txt AC 128 ms 25272 KB
test_29.txt AC 132 ms 25252 KB
test_30.txt AC 95 ms 19796 KB
test_31.txt AC 77 ms 19540 KB
test_32.txt AC 131 ms 24936 KB
test_33.txt AC 132 ms 21676 KB
test_34.txt AC 79 ms 17108 KB
test_35.txt AC 101 ms 21972 KB
test_36.txt AC 129 ms 23400 KB