結果
問題 | No.859 路線A、路線B、路線C |
ユーザー |
![]() |
提出日時 | 2023-03-15 14:03:07 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 55 ms / 1,000 ms |
コード長 | 3,126 bytes |
コンパイル時間 | 2,753 ms |
コンパイル使用メモリ | 85,172 KB |
実行使用メモリ | 50,428 KB |
最終ジャッジ日時 | 2024-09-18 08:43:57 |
合計ジャッジ時間 | 4,567 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
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();}}