結果

問題 No.943 取り調べ
ユーザー tnakao0123tnakao0123
提出日時 2019-12-07 19:02:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 118 ms / 1,206 ms
コード長 1,410 bytes
コンパイル時間 573 ms
コンパイル使用メモリ 82,876 KB
実行使用メモリ 4,696 KB
最終ジャッジ日時 2023-08-26 23:36:42
合計ジャッジ時間 2,927 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,376 KB
testcase_04 AC 118 ms
4,536 KB
testcase_05 AC 115 ms
4,696 KB
testcase_06 AC 114 ms
4,620 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 114 ms
4,564 KB
testcase_09 AC 24 ms
4,380 KB
testcase_10 AC 24 ms
4,380 KB
testcase_11 AC 11 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 24 ms
4,376 KB
testcase_18 AC 2 ms
4,376 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 24 ms
4,376 KB
testcase_23 AC 115 ms
4,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 943.cc:  No.943 取り調べ - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 18;
const int NBITS = 1 << MAX_N;
const int INF = 1 << 30;

/* typedef */

/* global variables */

int xbs[MAX_N], as[MAX_N], asums[NBITS];

/* subroutines */

bool check(int n, int bits) {
  for (int i = 0; i < n; i++)
    for (int j = 0, bj = 1; j < n; j++, bj <<= 1)
      if ((bits & xbs[j]) == xbs[j]) bits |= bj;
  return (bits == (1 << n) - 1);
}

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++)
    for (int j = 0, bj = 1; j < n; j++, bj <<= 1) {
      int x;
      scanf("%d", &x);
      if (x) xbs[i] |= bj;
    }

  for (int i = 0; i < n; i++) scanf("%d", as + i);

  int nbits = 1 << n;
  for (int bits = 1, msb = 1, bi = 0; bits < nbits; bits++) {
    if ((msb << 1) <= bits) msb <<= 1, bi++;
    asums[bits] = asums[bits ^ msb] + as[bi];
  }

  int minsum = INF;
  for (int bits = 0; bits < nbits; bits++)
    if (check(n, bits) && minsum > asums[bits]) minsum = asums[bits];

  printf("%d\n", minsum);
  return 0;
}
0