結果

問題 No.1690 Power Grid
ユーザー tentententen
提出日時 2023-04-13 09:52:27
言語 Java21
(openjdk 21)
結果
AC  
実行時間 263 ms / 3,000 ms
コード長 5,228 bytes
コンパイル時間 3,521 ms
コンパイル使用メモリ 85,612 KB
実行使用メモリ 60,988 KB
最終ジャッジ日時 2024-10-09 09:10:25
合計ジャッジ時間 7,982 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
50,424 KB
testcase_01 AC 59 ms
50,160 KB
testcase_02 AC 59 ms
50,508 KB
testcase_03 AC 57 ms
50,304 KB
testcase_04 AC 56 ms
50,428 KB
testcase_05 AC 61 ms
50,556 KB
testcase_06 AC 228 ms
58,472 KB
testcase_07 AC 239 ms
60,988 KB
testcase_08 AC 216 ms
58,424 KB
testcase_09 AC 216 ms
58,556 KB
testcase_10 AC 116 ms
54,840 KB
testcase_11 AC 125 ms
55,500 KB
testcase_12 AC 60 ms
50,612 KB
testcase_13 AC 60 ms
50,668 KB
testcase_14 AC 137 ms
53,464 KB
testcase_15 AC 94 ms
54,292 KB
testcase_16 AC 241 ms
58,360 KB
testcase_17 AC 192 ms
56,288 KB
testcase_18 AC 159 ms
54,300 KB
testcase_19 AC 260 ms
58,640 KB
testcase_20 AC 263 ms
58,572 KB
testcase_21 AC 113 ms
54,888 KB
testcase_22 AC 192 ms
56,332 KB
testcase_23 AC 93 ms
54,356 KB
testcase_24 AC 127 ms
55,332 KB
testcase_25 AC 116 ms
54,700 KB
testcase_26 AC 117 ms
54,900 KB
testcase_27 AC 57 ms
50,168 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 k = sc.nextInt();
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[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] = c;
            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]);
                }
            }
        }
        ArrayList<Bridge> bridges = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                bridges.add(new Bridge(i, j, distances[i][j]));
            }
        }
        Collections.sort(bridges);
        long ans = Long.MAX_VALUE;
        UnionFindTree uft = new UnionFindTree(n);
        long[] dp = new long[1 << n];
        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;
                }
                dp[i] = dp[i ^ (1 << j)] + values[j];
                break;
            }
            if (pop != k) {
                continue;
            }
            long current = dp[i];
            uft.clear();
            for (Bridge x : bridges) {
                if (x.matches(i) && !uft.same(x)) {
                    uft.unite(x);
                    current += x.value;
                }
            }
            ans = Math.min(ans, current);
        }
        System.out.println(ans);
    }
    
    static int getPop(int x) {
        int pop = 0;
        while (x > 0) {
            pop += x % 2;
            x >>= 1;
        }
        return pop;
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int size) {
            parents = new int[size];
        }
        
        public void clear() {
            for (int i = 0; i < parents.length; i++) {
                parents[i] = i;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public boolean same(Bridge x) {
            return same(x.left, x.right);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                parents[find(x)] = find(y);
            }
        }
        
        public void unite(Bridge x) {
            unite(x.left, x.right);
        }
    }
    
    static class Bridge implements Comparable<Bridge> {
        int left;
        int right;
        long value;
        
        public Bridge(int left, int right, long value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }
        
        public int compareTo(Bridge another) {
            if (value == another.value) {
                return 0;
            } else if (value < another.value) {
                return -1;
            } else {
                return 1;
            }
        }
        
        public boolean matches(int mask) {
            return ((mask & (1 << left)) > 0) && ((mask & (1 << right)) > 0);
        }
    }
}
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