题目连接:

https://ac.nowcoder.com/acm/contest/882/D

Description

Given a vertex-weighted graph with N vertices, find out the K-th minimum weighted clique.

A subset of vertices of an undirected graph is called clique if and only if every two distinct vertices in the subset are adjacent. The weight of a clique is the summation of the weight of vertices in it.

Input

The first line of input contains two space-separated integers N, K.

The second line of input contains N space-separated integers wi representing the weight of each vertex.

Following N lines each contains N characters eij. There's an edge between vertex i and vertex j if and only if eij = "1".

1 <= N <= 100

1 <= K <= 10^6

0 <= wi <= 10^9

Output

Output one line containing an integer representing the answer. If there's less than K cliques, output "-1".

Sample Input

2 3

1 2

01

10

Sample Output

2

Hint

题意

给出一个图的邻接矩阵,求该图的所有完全子图(点属于该图且任意两点有边)中权值第K小的价值,图权值为图中所有点的权值之和

题解:

用类似bfs的策略从0个点开始搜索,利用优先队列每次取出价值最小的子图,第K次出队的子图就是第K小的

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <bitset> using namespace std;
typedef long long ll;
const int mx = 105;
ll v[mx];
int n, k;
int mp[mx][mx];
bitset<mx> sum[mx]; struct Node {
bitset<mx> state;
ll value;
int last;
bool operator < (Node other) const {
return value > other.value;
}
}; ll solve() {
priority_queue <Node> Q;
Node st;
st.state.reset();
st.value = 0;
st.last = 0;
Q.push(st);
while (!Q.empty()) {
k--;
Node now = Q.top();
Node next = now;
Q.pop();
if (k == 0) return now.value;
for (int i = now.last+1; i <= n; i++) {
if ((now.state & sum[i]) == now.state) {
next = now;
next.state[i] = 1;
next.value += v[i];
next.last = i;
Q.push(next);
}
}
}
return -1;
} char str[mx]; int main() {
scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%lld", &v[i]);
for (int i = 1; i <= n; i++) {
scanf("%s", str+1);
for (int j = 1; j <= n; j++) {
mp[i][j] = str[j]-'0';
if (mp[i][j]) sum[i][j] = 1;
}
}
printf("%lld\n", solve());
return 0;
}

最新文章

  1. js new
  2. iOS中的事件传递和响应者链条
  3. Linux下安装部署Java
  4. Linux:挂载外部U盘,移动数据
  5. DBA常用SQL之DDL生成语句
  6. HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))
  7. NET笔试题集
  8. typedef和define的作用域
  9. C++ 表达式语句 海伦的故事
  10. 魔兽世界服务器Trinitycore分析二:auth server的main函数
  11. doubango(6)--Doubango协议栈中对RTP的管理
  12. Linux实战教学笔记18:linux三剑客之awk精讲
  13. 毕业设计(4):基于MicroPython的超声波倒车雷达系统
  14. 事务回滚 try catch
  15. wps直接打开CVS文件会把长串数字订单号最后4位变为0
  16. Liunx touch
  17. windows下nodejs监听80端口
  18. spring 多线程
  19. c++ list erase()
  20. Java RSA公钥加密,私钥解密算法的尝试

热门文章

  1. Metrics类型
  2. hdoj 1753 (Java)
  3. SpringBoot Jar包瘦身 - 跟大文件说再见!
  4. Intent 常用方法总结
  5. python_0基础开始_day05
  6. JVM系列(1)- JVM常见参数及堆内存分配
  7. 使用抽象工厂反射获取不到Dal层对象,未能加载文件或程序集......
  8. JAVA基础知识(七)存根类
  9. JavaWeb——JSP开发1
  10. 如何搭建环境---初识mybatis