結果

問題 No.1690 Power Grid
ユーザー square1001square1001
提出日時 2021-09-24 21:34:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 191 ms / 3,000 ms
コード長 1,333 bytes
コンパイル時間 701 ms
コンパイル使用メモリ 76,864 KB
実行使用メモリ 6,296 KB
最終ジャッジ日時 2023-09-18 20:49:54
合計ジャッジ時間 4,938 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 191 ms
6,164 KB
testcase_07 AC 190 ms
6,292 KB
testcase_08 AC 190 ms
6,132 KB
testcase_09 AC 190 ms
6,136 KB
testcase_10 AC 190 ms
6,136 KB
testcase_11 AC 190 ms
6,140 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 20 ms
4,376 KB
testcase_15 AC 191 ms
6,240 KB
testcase_16 AC 191 ms
6,192 KB
testcase_17 AC 87 ms
4,708 KB
testcase_18 AC 42 ms
4,376 KB
testcase_19 AC 190 ms
6,296 KB
testcase_20 AC 191 ms
6,180 KB
testcase_21 AC 191 ms
6,276 KB
testcase_22 AC 191 ms
6,184 KB
testcase_23 AC 190 ms
6,296 KB
testcase_24 AC 190 ms
6,276 KB
testcase_25 AC 190 ms
6,180 KB
testcase_26 AC 191 ms
6,268 KB
testcase_27 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
	int N, M, K;
	cin >> N >> M >> K;
	vector<long long> A(N);
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
	}
	const long long INF = 1LL << 61;
	vector<vector<long long> > d(N, vector<long long>(N, INF));
	for (int i = 0; i < N; ++i) {
		d[i][i] = 0;
	}
	for (int i = 0; i < M; ++i) {
		int a, b; long long c;
		cin >> a >> b >> c;
		--a, --b;
		d[a][b] = c;
		d[b][a] = c;
	}
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			for (int k = 0; k < N; ++k) {
				d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
			}
		}
	}
	vector<long long> dp(1 << N, INF);
	dp[0] = 0;
	for (int i = 1; i < (1 << N); ++i) {
		for (int j = 0; j < N; ++j) {
			if (((i >> j) & 1) == 0) {
				continue;
			}
			int sub = i - (1 << j);
			long long cost = dp[sub] + A[j];
			if (sub != 0) {
				long long mindist = INF;
				for (int k = 0; k < N; ++k) {
					if (((sub >> k) & 1) == 1) {
						mindist = min(mindist, d[j][k]);
					}
				}
				cost += mindist;
			}
			dp[i] = min(dp[i], cost);
		}
	}
	vector<int> pop(1 << N);
	for (int i = 1; i < (1 << N); ++i) {
		pop[i] = pop[i >> 1] + (i & 1);
	}
	long long ans = INF;
	for (int i = 0; i < (1 << N); ++i) {
		if (pop[i] == K) {
			ans = min(ans, dp[i]);
		}
	}
	cout << ans << endl;
	return 0;
}
0