結果
| 問題 | No.943 取り調べ |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-12-09 07:46:36 |
| 言語 | C++11 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 117 ms / 1,206 ms |
| + 330µs | |
| コード長 | 1,557 bytes |
| 記録 | |
| コンパイル時間 | 314 ms |
| コンパイル使用メモリ | 66,520 KB |
| 実行使用メモリ | 5,888 KB |
| 最終ジャッジ日時 | 2026-07-25 16:42:46 |
| 合計ジャッジ時間 | 2,109 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge3_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 |
ソースコード
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));
}