2019牛客暑期多校训练营(第二场)-D Kth Minimum Clique

题目链接:https://ac.nowcoder.com/acm/contest/882/D

题意:求给定点权无向图中,点权和第k小的完全子图的点权和。(包括空集)

思路:从空集开始,每找到一个完全子图,通过添加一个点来找到新的完全子图(只要该点与原来的所有点邻接),并存入优先队列中,每次取出权值和最小的来更新。用bitset来存储当前完全子图中存了哪些点,为了避免更新重复的子图,需要记录每个状态上一次添加的是哪个点,下次遍历该点之后的点,从而防止重复。

AC代码:

#include<cstdio>
#include<algorithm>
#include<bitset>
#include<queue>
using namespace std;

typedef long long LL;
typedef bitset<105> BS;

struct node{
    LL sum;
    int pos;
    BS vis;
    node(){}
    node(LL s,int p,BS v){
        this->sum=s;
        this->pos=p;
        this->vis=v;
    }
    bool operator < (const node& other) const{
        return sum>other.sum;
    }
};

int n,k;
LL a[105];
BS b[105];
char s[105];

int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;++i)
        scanf("%lld",&a[i]);
    for(int i=1;i<=n;++i){
        scanf("%s",s);
        for(int j=1;j<=n;++j)
            b[i][j]=s[j-1]-'0';
    }
    BS tmp;
    tmp.reset();
    priority_queue<node> pq;
    pq.push(node(0,1,tmp));
    while(!pq.empty()){
        node now=pq.top();pq.pop();
        if(--k==0){
            printf("%lld
",now.sum);
            return 0;
        }
        for(int i=now.pos;i<=n;++i)
            if((b[i]&now.vis)==now.vis){
                now.vis[i]=1;
                pq.push(node(now.sum+a[i],i+1,now.vis));
                now.vis[i]=0;
            }
    }
    printf("-1
");
    return 0;
}
原文地址:https://www.cnblogs.com/FrankChen831X/p/11228576.html