2019牛客暑期多校训练营(第二场)-D Kth Minimum Clique
2024-10-06 15:24:55
题目链接: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<> 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[];
BS b[];
char s[]; int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=n;++i)
scanf("%lld",&a[i]);
for(int i=;i<=n;++i){
scanf("%s",s);
for(int j=;j<=n;++j)
b[i][j]=s[j-]-'';
}
BS tmp;
tmp.reset();
priority_queue<node> pq;
pq.push(node(,,tmp));
while(!pq.empty()){
node now=pq.top();pq.pop();
if(--k==){
printf("%lld\n",now.sum);
return ;
}
for(int i=now.pos;i<=n;++i)
if((b[i]&now.vis)==now.vis){
now.vis[i]=;
pq.push(node(now.sum+a[i],i+,now.vis));
now.vis[i]=;
}
}
printf("-1\n");
return ;
}
最新文章
- 一次发现underscore源码bug的经历以及对学术界『拿来主义』的思考
- Linux守护进程
- 泛函编程(28)-粗俗浅解:Functor, Applicative, Monad
- HTML动画分类 HTML5动画 SVG库 SVG工具 Canvas动画工具
- 【网络】VPN
- 获取ip
- CRM 权限与分派不一样问题
- jquery源码学习之extend
- 安装及破解IntelliJ IDEA15
- 自己diy一个jquery分页插件
- Provisioning Profile
- STL之map
- c++中基本的语法问题
- ASP.Net TextBox控件只允许输入数字
- java读写分离的实现
- nginx学习笔记(二)
- OS模块常用方法
- Knockout.Js官网学习(selectedOptions绑定、uniqueName 绑定)
- django人类可读性
- 服务网关Zuul