結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
50,120 KB
testcase_01 AC 45 ms
50,212 KB
testcase_02 AC 45 ms
50,412 KB
testcase_03 AC 48 ms
50,260 KB
testcase_04 AC 45 ms
50,272 KB
testcase_05 AC 44 ms
50,364 KB
testcase_06 AC 239 ms
60,828 KB
testcase_07 AC 203 ms
58,576 KB
testcase_08 AC 213 ms
56,776 KB
testcase_09 AC 242 ms
60,672 KB
testcase_10 AC 107 ms
55,056 KB
testcase_11 AC 107 ms
54,716 KB
testcase_12 AC 47 ms
50,560 KB
testcase_13 AC 50 ms
50,404 KB
testcase_14 AC 115 ms
53,592 KB
testcase_15 AC 100 ms
54,532 KB
testcase_16 AC 202 ms
58,612 KB
testcase_17 AC 164 ms
56,424 KB
testcase_18 AC 130 ms
54,444 KB
testcase_19 AC 278 ms
56,904 KB
testcase_20 AC 257 ms
56,588 KB
testcase_21 AC 99 ms
55,036 KB
testcase_22 AC 175 ms
56,516 KB
testcase_23 AC 88 ms
54,116 KB
testcase_24 AC 122 ms
55,424 KB
testcase_25 AC 113 ms
54,980 KB
testcase_26 AC 112 ms
54,644 KB
testcase_27 AC 50 ms
50,408 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