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

题目连接:https://ac.nowcoder.com/acm/contest/882/Dnode

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

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

AC代码:code

#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\n",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\n");
    return 0;
}
相关文章
相关标签/搜索