結果

問題 No.943 取り調べ
ユーザー ianCKianCK
提出日時 2019-12-09 07:46:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 232 ms / 1,206 ms
コード長 1,557 bytes
コンパイル時間 383 ms
コンパイル使用メモリ 45,508 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-02 12:24:25
合計ジャッジ時間 3,001 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 165 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 166 ms
4,380 KB
testcase_09 AC 39 ms
4,376 KB
testcase_10 AC 37 ms
4,376 KB
testcase_11 AC 17 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 35 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 36 ms
4,376 KB
testcase_23 AC 232 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

bool debug = false;
#include <stdio.h>
#include <queue>
using namespace std;

constexpr int kN = 1 << 18, kInf = int(1E9 + 10), kC = 18;

int a[kC], g[kC][kC], dp[kN], cost, n, d[kC], td[kC];
bool have[kC], used[kC];

bool ok() {
	queue<int> q;
	int nxt;
	for (int i = 0; i < n; i++) used[i] = have[i] | (d[i] == 0);
	for (int i = 0; i < n; i++) if (used[i]) q.push(i);
	for (int i = 0; i < n; i++) td[i] = d[i];
	while (!q.empty()) {
		nxt = q.front();
		q.pop();
		for (int i = 0; i < n; i++) if (g[i][nxt]) td[i]--;
		for (int i = 0; i < n; i++) if (td[i] == 0 && !used[i]) {
			used[i] = true;
			q.push(i);
		}
	}
	if (debug) {
		printf("--debug--\n");
		for (int i = 0; i < n; i++) printf("%d", have[i] ? 1 : 0);
		printf("\n");
		for (int i = 0; i < n; i++) printf("%d", used[i] ? 1 : 0);
		printf("\n\n");
	}
	for (int i = 0; i < n; i++) if (!used[i]) return false;
	return true;
}

int dfs(int pos) {
	if (ok()) return cost;
	if (pos >= n) return kInf;
	int ans = kInf;
	for (int i = pos; i < n; i++) {
		have[i] = true;
		cost += a[i];
		ans = min(ans, dfs(i + 1));
		have[i] = false;
		cost -= a[i];
	}
	return ans;
}

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", &g[i][j]);
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);
	for (int i = 0; i < n; i++) d[i] = 0;
	for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (g[i][j] == 1) d[i]++;
	cost = 0;
	for (int i = 0; i < n; i++) have[i] = false;	
	printf("%d\n", dfs(0));
}
0