結果

問題 No.943 取り調べ
ユーザー 👑 emthrmemthrm
提出日時 2019-12-06 01:45:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,129 bytes
コンパイル時間 2,342 ms
コンパイル使用メモリ 145,604 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 19:33:48
合計ジャッジ時間 8,665 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 TLE -
testcase_05 AC 268 ms
4,380 KB
testcase_06 AC 268 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 TLE -
testcase_09 AC 211 ms
4,380 KB
testcase_10 AC 228 ms
4,384 KB
testcase_11 AC 122 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 316 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 355 ms
4,384 KB
testcase_23 AC 517 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1},
//           dx[] = {0, -1, -1, -1, 0, 1, 1, 1};

struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(10);
  }
} iosetup;
/*-------------------------------------------------*/
using CostType = long long;
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &rhs) const {
    return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
  }
  inline bool operator<=(const Edge &rhs) const { return !(rhs < *this); }
  inline bool operator>(const Edge &rhs) const { return rhs < *this; }
  inline bool operator>=(const Edge &rhs) const { return !(*this < rhs); }
};

vector<int> topological_sort(const vector<vector<Edge> > &graph) {
  int n = graph.size();
  vector<int> deg(n, 0);
  REP(i, n) {
    for (const Edge &e : graph[i]) ++deg[e.dst];
  }
  queue<int> que;
  REP(i, n) {
    if (deg[i] == 0) que.emplace(i);
  }
  vector<int> res;
  while (!que.empty()) {
    int ver = que.front(); que.pop();
    res.emplace_back(ver);
    for (const Edge &e : graph[ver]) {
      if (--deg[e.dst] == 0) que.emplace(e.dst);
    }
  }
  return res.size() == n ? res : vector<int>();
};

int main() {
  int n; cin >> n;
  vector<vector<int> > x(n, vector<int>(n)); REP(i, n) REP(j, n) cin >> x[i][j];
  vector<int> a(n); REP(i, n) cin >> a[i];
  int ans = accumulate(ALL(a), 0);
  vector<bool> ok(1 << n, false);
  REP(i, (1 << n) - 1) {
    if (ok[i]) continue;
    vector<bool> conf(n, false);
    REP(j, n) if (i >> j & 1) conf[j] = true;
    vector<vector<Edge> > graph;
    map<int, int> mp;
    REP(i, n) {
      if (!conf[i]) {
        int sz = mp.size();
        mp[i] = sz;
        graph.emplace_back();
      }
    }
    REP(i, n) {
      if (conf[i]) continue;
      REP(j, n) {
        if (x[i][j] == 1 && mp.count(j) == 1) graph[mp[j]].emplace_back(mp[j], mp[i]);
      }
    }
    vector<int> ts = topological_sort(graph);
    if (ts.empty()) continue;
    int tmp = 0;
    REP(j, n) if (i >> j & 1) tmp += a[j];
    ans = min(ans, tmp);
    REP(j, n) if (!(i >> j & 1)) ok[i | (1 << j)] = true;
  }
  cout << ans << '\n';
  return 0;
}
0