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