题目链接
bfs+bitset优化
从空集开始不断的向集合内加点,并记录总消费cost,用优先队列优先弹出cost最低的集合,向集合中加入新的点,加入点的要满足与集合内所有的点都有边,使用bitset存储集合。另外避免状态重复,每次扩展新点都从最大编号开始,向n扩展。
#include <iostream>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <bitset>
using namespace std;
typedef long long ll;
const int Maxn = 100+10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
struct Node {
bitset <Maxn> stat;
ll cost;
bool operator < (const Node &a1) const {
return cost > a1.cost;
}
};
bitset <Maxn> G[Maxn];
int cost[Maxn], n, k;
int main(void)
{
scanf("%d%d", &n, &k);
for(int i = 0; i < n; ++i) scanf("%d", &cost[i]);
char s[Maxn];
for(int i = 0; i < n; ++i) {
scanf("%s", s);
for(int j = 0; j < n; ++j) {
if(s[j] == '1') G[i].set(j);
}
}
priority_queue <Node> qu;
Node tmp;
tmp.cost = 0; tmp.stat.reset();
qu.push(tmp);
ll ans = -1;
while(!qu.empty()) {
tmp = qu.top(); qu.pop();
if(--k == 0) {
ans = tmp.cost;
break;
}
int pos = 0;
for(int i = n; i > 0; --i) if(tmp.stat[i-1] == 1) {
pos = i; break;
}
Node cur;
for(int i = pos; i < n; ++i) {
if((tmp.stat&G[i]) == tmp.stat) {
cur.cost = tmp.cost+cost[i];
cur.stat = tmp.stat;
cur.stat.set(i);
qu.push(cur);
}
}
}
printf("%lld\n", ans);
return 0;
}