题目链接: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 ;
}

最新文章

  1. 一次发现underscore源码bug的经历以及对学术界『拿来主义』的思考
  2. Linux守护进程
  3. 泛函编程(28)-粗俗浅解:Functor, Applicative, Monad
  4. HTML动画分类 HTML5动画 SVG库 SVG工具 Canvas动画工具
  5. 【网络】VPN
  6. 获取ip
  7. CRM 权限与分派不一样问题
  8. jquery源码学习之extend
  9. 安装及破解IntelliJ IDEA15
  10. 自己diy一个jquery分页插件
  11. Provisioning Profile
  12. STL之map
  13. c++中基本的语法问题
  14. ASP.Net TextBox控件只允许输入数字
  15. java读写分离的实现
  16. nginx学习笔记(二)
  17. OS模块常用方法
  18. Knockout.Js官网学习(selectedOptions绑定、uniqueName 绑定)
  19. django人类可读性
  20. 服务网关Zuul

热门文章

  1. GDIPlus的使用准备工作
  2. 《剑指offer》算法题第十一天
  3. @RequestMapping的简单理解
  4. php+列出目录文件
  5. UNIX下socket通信 - UDP通信
  6. monkey test——学习资料
  7. k8s集群节点更换ip 或者 k8s集群添加新节点
  8. jQuery文档操作之插入操作
  9. 【知识库】-数据库_MySQL常用SQL语句语法大全示例
  10. 用 Docker 搭建 ORACLE 数据库开发环境