結果

問題 No.1690 Power Grid
ユーザー ks2mks2m
提出日時 2021-09-24 22:43:45
言語 Java19
(openjdk 21)
結果
AC  
実行時間 543 ms / 3,000 ms
コード長 2,682 bytes
コンパイル時間 2,699 ms
コンパイル使用メモリ 92,236 KB
実行使用メモリ 63,496 KB
最終ジャッジ日時 2023-09-18 21:52:14
合計ジャッジ時間 11,921 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
54,052 KB
testcase_01 AC 127 ms
55,876 KB
testcase_02 AC 132 ms
56,016 KB
testcase_03 AC 126 ms
55,632 KB
testcase_04 AC 126 ms
55,648 KB
testcase_05 AC 126 ms
56,036 KB
testcase_06 AC 531 ms
62,564 KB
testcase_07 AC 518 ms
62,848 KB
testcase_08 AC 452 ms
62,528 KB
testcase_09 AC 464 ms
62,472 KB
testcase_10 AC 183 ms
57,248 KB
testcase_11 AC 185 ms
57,084 KB
testcase_12 AC 129 ms
55,736 KB
testcase_13 AC 145 ms
55,860 KB
testcase_14 AC 278 ms
62,476 KB
testcase_15 AC 201 ms
56,880 KB
testcase_16 AC 543 ms
63,404 KB
testcase_17 AC 420 ms
62,852 KB
testcase_18 AC 354 ms
62,792 KB
testcase_19 AC 504 ms
63,176 KB
testcase_20 AC 538 ms
62,816 KB
testcase_21 AC 167 ms
57,932 KB
testcase_22 AC 364 ms
63,496 KB
testcase_23 AC 146 ms
55,576 KB
testcase_24 AC 211 ms
58,448 KB
testcase_25 AC 192 ms
56,792 KB
testcase_26 AC 188 ms
56,856 KB
testcase_27 AC 126 ms
55,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}

		long inf = 1000000000000L;
		long[][] d = new long[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (i != j) {
					d[i][j] = inf;
				}
			}
		}
		for (int i = 0; i < m; i++) {
			int x = sc.nextInt() - 1;
			int y = sc.nextInt() - 1;
			int z = sc.nextInt();
			d[x][y] = z;
			d[y][x] = z;
		}
		for (int p = 0; p < n; p++) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					if (d[i][p] != inf && d[p][j] != inf) {
						d[i][j] = Math.min(d[i][j], d[i][p] + d[p][j]);
					}
				}
			}
		}
		sc.close();

		List<Obj> list = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (i != j) {
					Obj o = new Obj();
					o.x = i;
					o.y = j;
					o.z = d[i][j];
					list.add(o);
				}
			}
		}
		Collections.sort(list, (o1, o2) -> Long.compare(o1.z, o2.z));

		long ans = Long.MAX_VALUE;
		int end = 1 << n;
		for (int i = 0; i < end; i++) {
			if (Integer.bitCount(i) != k) {
				continue;
			}
			PriorityQueue<Integer> que = new PriorityQueue<>();
			for (int j = 0; j < n; j++) {
				if ((i >> j & 1) == 1) {
					que.add(a[j]);
				}
			}
			long val = 0;
			for (int j = 0; j < k; j++) {
				val += que.poll();
			}

			UnionFind uf = new UnionFind(n);
			for (Obj o : list) {
				if ((i >> o.x & 1) == 1 && (i >> o.y & 1) == 1
						&& !uf.same(o.x, o.y)) {
					uf.union(o.x, o.y);
					val += o.z;
				}
			}
			ans = Math.min(ans, val);
		}
		System.out.println(ans);
	}

	static class Obj {
		int x, y;
		long z;
	}

	static class UnionFind {
		int[] parent, size;
		int num = 0; // 連結成分の数

		UnionFind(int n) {
			parent = new int[n];
			size = new int[n];
			num = n;
			for (int i = 0; i < n; i++) {
				parent[i] = i;
				size[i] = 1;
			}
		}

		void union(int x, int y) {
			int px = find(x);
			int py = find(y);
			if (px != py) {
				parent[px] = py;
				size[py] += size[px];
				num--;
			}
		}

		int find(int x) {
			if (parent[x] == x) {
				return x;
			}
			parent[x] = find(parent[x]);
			return parent[x];
		}

		/**
		 * xとyが同一連結成分か
		 */
		boolean same(int x, int y) {
			return find(x) == find(y);
		}

		/**
		 * xを含む連結成分のサイズ
		 */
		int size(int x) {
			return size[find(x)];
		}
	}
}
0