結果

問題 No.1690 Power Grid
ユーザー tenten
提出日時 2024-08-08 10:44:41
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 3,142 bytes
コンパイル時間 2,525 ms
コンパイル使用メモリ 79,236 KB
実行使用メモリ 55,128 KB
最終ジャッジ日時 2024-08-08 10:44:48
合計ジャッジ時間 6,673 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
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 i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int a = 0; a < n; a++) {
                    distances[j][a] = Math.min(distances[j][a], distances[j][i] + distances[i][a]);
                }
            }
        }
        long[] dp = new long[1 << n];
        long ans = Long.MAX_VALUE;
        for (int i = 1; i < (1 << n); i++) {
            int pop = getPop(i);
            if (pop > k) {
                continue;
            }
            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;
            }
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                long current = 0;
                for (int a = 0; a < n; a++) {
                    if (j == a || (i & (1 << a)) == 0) {
                        continue;
                    }
                    current += distances[j][a];
                }
                ans = Math.min(ans, dp[i] + current);
            }
        }
        System.out.println(ans);
    }
    
    static int getPop(int x) {
        int pop = 0;
        while (x > 0) {
            pop += x % 2;
            x >>= 1;
        }
        return pop;
    } 
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}


0