結果

問題 No.1690 Power Grid
ユーザー tentententen
提出日時 2023-02-03 16:49:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 600 ms / 3,000 ms
コード長 4,946 bytes
コンパイル時間 2,785 ms
コンパイル使用メモリ 80,940 KB
実行使用メモリ 59,464 KB
最終ジャッジ日時 2023-09-15 12:13:20
合計ジャッジ時間 9,941 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
49,980 KB
testcase_01 AC 47 ms
49,580 KB
testcase_02 AC 48 ms
49,960 KB
testcase_03 AC 46 ms
49,452 KB
testcase_04 AC 46 ms
49,468 KB
testcase_05 AC 45 ms
49,588 KB
testcase_06 AC 315 ms
58,064 KB
testcase_07 AC 271 ms
57,336 KB
testcase_08 AC 270 ms
57,368 KB
testcase_09 AC 279 ms
57,296 KB
testcase_10 AC 88 ms
52,904 KB
testcase_11 AC 98 ms
53,408 KB
testcase_12 AC 47 ms
49,688 KB
testcase_13 AC 55 ms
49,596 KB
testcase_14 AC 154 ms
56,512 KB
testcase_15 AC 73 ms
52,236 KB
testcase_16 AC 399 ms
57,932 KB
testcase_17 AC 354 ms
58,948 KB
testcase_18 AC 229 ms
57,920 KB
testcase_19 AC 600 ms
59,464 KB
testcase_20 AC 443 ms
58,872 KB
testcase_21 AC 91 ms
52,636 KB
testcase_22 AC 265 ms
57,740 KB
testcase_23 AC 71 ms
52,112 KB
testcase_24 AC 151 ms
57,512 KB
testcase_25 AC 117 ms
53,412 KB
testcase_26 AC 107 ms
53,712 KB
testcase_27 AC 46 ms
49,992 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 h = sc.nextInt();
        int[] values = new int[n];
        long[][] costs = new long[n][n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            Arrays.fill(costs[i], Long.MAX_VALUE / 2);
            costs[i][i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            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++) {
                    costs[j][k] = Math.min(costs[j][k], costs[j][i] + costs[i][k]);
                }
            }
        }
        UnionFindTree uft = new UnionFindTree(n);
        long ans = Long.MAX_VALUE;
        for (int i = 1; i < (1 << n); i++) {
            if (getPop(i) != h)  {
                continue;
            }
            PriorityQueue<Path> queue = new PriorityQueue<>();
            long current = 0;
            uft.clear();
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                current += values[j];
                for (int k = j + 1; k < n; k++) {
                    if ((i & (1 << k)) == 0) {
                        continue;
                    }
                    queue.add(new Path(j, k, costs[j][k]));
                }
            }
            while (queue.size() > 0) {
                Path p = queue.poll();
                if (!uft.same(p)) {
                    current += p.value;
                    uft.unite(p);
                }
            }
            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 Path implements Comparable<Path> {
        int left;
        int right;
        long value;
        
        public Path(int left, int right, long value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            if (value == another.value) {
                return 0;
            } else if (value < another.value) {
                return -1;
            } else {
                return 1;
            }
        }
    }
    
    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(Path p) {
            return same(p.left, p.right);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                parents[find(x)] = find(y);
            }
        }
        
        public void unite(Path p) {
            unite(p.left, p.right);
        }
    }
    
}
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