結果
| 問題 |
No.943 取り調べ
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2019-12-06 01:35:50 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,151 bytes |
| コンパイル時間 | 1,818 ms |
| コンパイル使用メモリ | 133,328 KB |
| 最終ジャッジ日時 | 2025-01-08 08:57:55 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 TLE * 2 |
ソースコード
#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);
REP(i, (1 << n) - 1) {
vector<bool> conf(n, false);
REP(j, n) if (i >> j & 1) conf[j] = true;
vector<vector<Edge> > graph(n);
REP(i, n) {
if (conf[i]) continue;
REP(j, n) {
if (x[i][j] == 1) graph[j].emplace_back(j, i);
}
}
vector<int> ts = topological_sort(graph);
if (ts.empty()) continue;
bool cruel = true;
REP(j, n) {
int now = ts[j];
if (conf[now]) continue;
bool prop = false;
REP(k, n) {
if (x[now][k] == 1 && !conf[k]) prop = true;
}
if (prop) {
cruel = false;
break;
}
conf[now] = true;
}
if (cruel) {
int tmp = 0;
REP(j, n) if (i >> j & 1) tmp += a[j];
ans = min(ans, tmp);
}
}
cout << ans << '\n';
return 0;
}
emthrm