题意:求第k小团
题解:用优先队列存状态, bitset存团中点的状态
每次选取最后一个标记点的下一个这样保证不重复
#include <bits/stdc++.h>
using namespace std;
typedef bitset<105> bs;
typedef long long ll;
int n,k;
ll w[1000005];
bitset<105> f[105];
struct pi {
ll ans;
bs s;
bool operator < (const pi &c)const {
return ans>c.ans;
}
};
priority_queue<pi> q;
int main() {
scanf("%d %d",&n,&k);
for(int i=1; i<=n; i++) {
scanf("%d",&w[i]);
}
getchar();
char tp;
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
scanf("%c",&tp);
if(tp=='1')f[i][j] = 1;
}
getchar();
}
bs t; t.reset();
q.push((pi){0,t});
while(!q.empty()) {
pi tmp = q.top();
q.pop();
k--;
bs r = tmp.s;
int pos = 1; for(int i=1;i<=n;i++)if(r[i])pos = i+1;
if(k==0) {
printf("%lld\n",tmp.ans);
return 0;
}
for(int i=pos; i<=n; i++) {
if(!r[i]&&((r&f[i])==r)) {
r[i] = 1;
q.push((pi) {tmp.ans+w[i],r});
r[i] = 0;
}
}
}
printf("-1\n");
return 0;
}