結果

問題 No.859 路線A、路線B、路線C
ユーザー tentententen
提出日時 2023-01-10 16:41:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 45 ms / 1,000 ms
コード長 3,119 bytes
コンパイル時間 2,359 ms
コンパイル使用メモリ 88,552 KB
実行使用メモリ 49,800 KB
最終ジャッジ日時 2023-08-23 10:14:21
合計ジャッジ時間 3,793 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,192 KB
testcase_01 AC 44 ms
49,336 KB
testcase_02 AC 45 ms
49,224 KB
testcase_03 AC 44 ms
49,344 KB
testcase_04 AC 43 ms
49,496 KB
testcase_05 AC 44 ms
49,560 KB
testcase_06 AC 44 ms
49,408 KB
testcase_07 AC 43 ms
49,232 KB
testcase_08 AC 43 ms
49,288 KB
testcase_09 AC 43 ms
49,400 KB
testcase_10 AC 44 ms
49,308 KB
testcase_11 AC 44 ms
49,800 KB
testcase_12 AC 43 ms
49,704 KB
testcase_13 AC 43 ms
49,404 KB
testcase_14 AC 43 ms
49,176 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[] counts = new int[3];
        for (int i = 0; i < 3; i++) {
            counts[i] = sc.nextInt();
        }
        int[] target = new int[2];
        int[] station = new int[2];
        for (int i = 0; i < 2; i++) {
            target[i] = sc.next().charAt(0) - 'A';
            station[i] = sc.nextInt();
        }
        long[][] distance = new long[8][8];
        for (int i = 0; i < 8; i++) {
            Arrays.fill(distance[i], Long.MAX_VALUE / 2);
            distance[i][i] = 0;
        }
        for (int i = 0; i < 3; i++) {
            distance[i * 2][i * 2 + 1] = counts[i] - 1;
            distance[i * 2 + 1][i * 2] = counts[i] - 1;
            distance[i * 2][(i + 1) % 3 * 2] = 1;
            distance[i * 2][(i + 2) % 3 * 2] = 1;
            distance[i * 2 + 1][(i + 1) % 3 * 2 + 1] = 1;
            distance[i * 2 + 1][(i + 2) % 3 * 2 + 1] = 1;
        }
        for (int i = 0; i < 2; i++) {
            distance[target[i] * 2][6 + i] = station[i] - 1;
            distance[6 + i][target[i] * 2] = station[i] - 1;
            distance[target[i] * 2 + 1][6 + i] = counts[target[i]] - station[i];
            distance[6 + i][target[i] * 2 + 1] = counts[target[i]] - station[i];
        }
        if (target[0] == target[1]) {
            distance[6][7] = Math.abs(station[0] - station[1]);
            distance[7][6] = Math.abs(station[0] - station[1]);
        }
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                for (int k = 0; k < 8; k++) {
                    distance[j][k] = Math.min(distance[j][k], distance[j][i] + distance[i][k]);
                }
            }
        }
        System.out.println(distance[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