結果

問題 No.1690 Power Grid
ユーザー tentententen
提出日時 2023-06-28 12:56:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 374 ms / 3,000 ms
コード長 3,423 bytes
コンパイル時間 2,597 ms
コンパイル使用メモリ 86,604 KB
実行使用メモリ 57,232 KB
最終ジャッジ日時 2023-09-18 15:18:08
合計ジャッジ時間 10,198 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,328 KB
testcase_01 AC 44 ms
49,424 KB
testcase_02 AC 50 ms
49,604 KB
testcase_03 AC 45 ms
49,312 KB
testcase_04 AC 44 ms
49,200 KB
testcase_05 AC 45 ms
49,372 KB
testcase_06 AC 327 ms
54,348 KB
testcase_07 AC 309 ms
54,240 KB
testcase_08 AC 322 ms
54,088 KB
testcase_09 AC 341 ms
54,300 KB
testcase_10 AC 335 ms
53,988 KB
testcase_11 AC 374 ms
57,232 KB
testcase_12 AC 46 ms
49,720 KB
testcase_13 AC 50 ms
49,312 KB
testcase_14 AC 102 ms
52,196 KB
testcase_15 AC 361 ms
54,248 KB
testcase_16 AC 345 ms
54,248 KB
testcase_17 AC 199 ms
52,080 KB
testcase_18 AC 131 ms
52,416 KB
testcase_19 AC 335 ms
54,108 KB
testcase_20 AC 349 ms
54,608 KB
testcase_21 AC 364 ms
54,616 KB
testcase_22 AC 318 ms
54,588 KB
testcase_23 AC 322 ms
54,424 KB
testcase_24 AC 340 ms
54,052 KB
testcase_25 AC 346 ms
54,768 KB
testcase_26 AC 348 ms
54,388 KB
testcase_27 AC 46 ms
49,524 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 n = sc.nextInt();
        int m = sc.nextInt();
        int kk = sc.nextInt();
        int[] costs = new int[n];
        for (int i = 0; i < n; i++) {
            costs[i] = sc.nextInt();
        }
        long[][] distances = new long[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(distances[i], Long.MAX_VALUE / 2);
            distances[i][i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            distances[a][b] = Math.min(distances[a][b], c);
            distances[b][a] = Math.min(distances[b][a], c);
        }
        for (int a = 0; a < n; a++) {
            for (int b = 0; b < n; b++) {
                for (int c = 0; c < n; c++) {
                    distances[b][c] = Math.min(distances[b][c], distances[b][a] + distances[a][c]);
                }
            }
        }
        long[] dp = new long[1 << n];
        Arrays.fill(dp, Long.MAX_VALUE);
        dp[0] = 0;
        long[] ans = new long[n + 1];
        Arrays.fill(ans, Long.MAX_VALUE);
        ans[0] = 0;
        for (int i = 1; i < (1 << n); i++) {
            int pop = getPop(i);
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                if (pop == 1) {
                    dp[i] = costs[j];
                    break;
                }
                for (int k = 0; k < n; k++) {
                    if ((i & (1 << k)) == 0 || j == k) {
                        continue;
                    }
                    dp[i] = Math.min(dp[i], dp[i ^ (1 << j)] + distances[j][k] + costs[j]);
                }
            }
            ans[pop] = Math.min(ans[pop], dp[i]);
        }
        System.out.println(ans[kk]);
    }
    
    static int getPop(int x) {
        int pop = 0;
        while (x > 0) {
            pop += x % 2;
            x >>= 1;
        }
        return pop;
    }
}
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