結果

問題 No.17 2つの地点に泊まりたい
ユーザー tentententen
提出日時 2021-12-02 13:56:13
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,133 bytes
コンパイル時間 3,534 ms
コンパイル使用メモリ 74,104 KB
実行使用メモリ 52,928 KB
最終ジャッジ日時 2023-09-18 10:14:41
合計ジャッジ時間 5,183 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,288 KB
testcase_01 AC 43 ms
49,744 KB
testcase_02 AC 48 ms
49,148 KB
testcase_03 AC 100 ms
52,132 KB
testcase_04 AC 60 ms
50,528 KB
testcase_05 AC 112 ms
52,808 KB
testcase_06 AC 100 ms
52,512 KB
testcase_07 AC 96 ms
50,736 KB
testcase_08 AC 117 ms
52,480 KB
testcase_09 AC 118 ms
52,548 KB
testcase_10 AC 102 ms
52,536 KB
testcase_11 AC 111 ms
52,560 KB
testcase_12 AC 42 ms
49,308 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 106 ms
52,452 KB
testcase_24 AC 106 ms
52,244 KB
testcase_25 AC 46 ms
49,288 KB
testcase_26 AC 115 ms
52,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] stays = new int[n];
        for (int i = 0; i < n; i++) {
            stays[i] = sc.nextInt();
        }
        int m = sc.nextInt();
        int[][] costs = new int[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(costs[i], Integer.MAX_VALUE / 2);
            costs[i][i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            costs[a][b] = c;
            costs[b][a] = c;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    for (int l = 0; l < n; l++) {
                        costs[j][l] = Math.min(costs[j][l], costs[j][k] + costs[k][l]);
                    }
                }
            }
        }
        int min = Integer.MAX_VALUE;
        for (int i = 1; i < n - 2; i++) {
            for (int j = i + 1; j < n - 1; j++) {
                min = Math.min(min, stays[i] + stays[j] + costs[0][i] + costs[i][j] + costs[n - 1][j]);
                min = Math.min(min, stays[i] + stays[j] + costs[0][j] + costs[i][j] + costs[n - 1][i]);
            }
        }
        System.out.println(min);
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0