結果
| 問題 |
No.859 路線A、路線B、路線C
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-01-10 16:41:14 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 63 ms / 1,000 ms |
| コード長 | 3,119 bytes |
| コンパイル時間 | 2,821 ms |
| コンパイル使用メモリ | 91,868 KB |
| 実行使用メモリ | 50,452 KB |
| 最終ジャッジ日時 | 2024-12-21 12:14:12 |
| 合計ジャッジ時間 | 4,402 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[] 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();
}
}
tenten