結果

問題 No.859 路線A、路線B、路線C
ユーザー tentententen
提出日時 2023-03-15 14:03:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 56 ms / 1,000 ms
コード長 3,126 bytes
コンパイル時間 3,848 ms
コンパイル使用メモリ 89,560 KB
実行使用メモリ 53,488 KB
最終ジャッジ日時 2023-10-18 12:23:00
合計ジャッジ時間 5,515 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
53,488 KB
testcase_01 AC 54 ms
53,484 KB
testcase_02 AC 56 ms
53,476 KB
testcase_03 AC 55 ms
53,484 KB
testcase_04 AC 55 ms
53,472 KB
testcase_05 AC 54 ms
53,480 KB
testcase_06 AC 53 ms
53,488 KB
testcase_07 AC 54 ms
53,472 KB
testcase_08 AC 54 ms
53,484 KB
testcase_09 AC 54 ms
53,472 KB
testcase_10 AC 55 ms
53,484 KB
testcase_11 AC 55 ms
53,476 KB
testcase_12 AC 55 ms
53,480 KB
testcase_13 AC 55 ms
53,472 KB
testcase_14 AC 56 ms
53,488 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[] sizes = new int[3];
        for (int i = 0; i < 3; i++) {
            sizes[i] = sc.nextInt();
        }
        long[][] distances = new long[8][8];
        for (int i = 0; i < 8; i++) {
            Arrays.fill(distances[i], Long.MAX_VALUE / 2);
            distances[i][i] = 0;
        }
        for (int i = 0; i < 3; i++) {
            distances[i * 2][i * 2 + 1] = sizes[i] - 1;
            distances[i * 2 + 1][i * 2] = sizes[i] - 1;
        }
        for (int i = 0; i < 3; i++) {
            distances[i * 2][(i * 2 + 2) % 6] = 1;
            distances[(i * 2 + 2) % 6][i * 2] = 1;
            distances[i * 2 + 1][(i * 2 + 3) % 6] = 1;
            distances[(i * 2 + 3) % 6][i * 2 + 1] = 1;
        }
        int prevIdx = -1;
        int prevStation = -1;
        for (int i = 0; i < 2; i++) {
            int idx = sc.next().charAt(0) - 'A';
            int station = sc.nextInt();
            distances[6 + i][idx * 2] = station - 1;
            distances[idx * 2][6 + i] = station - 1;
            distances[6 + i][idx * 2 + 1] = sizes[idx] - station;
            distances[idx * 2 + 1][6 + i] = sizes[idx] - station;
            if (idx == prevIdx) {
                distances[6][7] = Math.abs(station - prevStation);
                distances[7][6] = Math.abs(station - prevStation);
            }
            prevIdx = idx;
            prevStation = station;
        }
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                for (int k = 0; k < 8; k++) {
                    distances[j][k] = Math.min(distances[j][k], distances[j][i] + distances[i][k]);
                }
            }
        }
        System.out.println(distances[6][7]);
    }
}
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