結果

問題 No.943 取り調べ
ユーザー tnakao0123
提出日時 2019-12-07 19:02:40
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 139 ms / 1,206 ms
コード長 1,410 bytes
コンパイル時間 834 ms
コンパイル使用メモリ 84,024 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-26 03:22:31
合計ジャッジ時間 2,903 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:52:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   52 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
main.cpp:57:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   57 |       scanf("%d", &x);
      |       ~~~~~^~~~~~~~~~
main.cpp:61:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   61 |   for (int i = 0; i < n; i++) scanf("%d", as + i);
      |                               ~~~~~^~~~~~~~~~~~~~

ソースコード

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