题目连接:

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;
}

Kth Minimum Clique_2019牛客暑期多校训练营(第二场)的更多相关文章

  1. 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)

    题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...

  2. 2020牛客暑期多校训练营 第二场 K Keyboard Free 积分 期望 数学

    LINK:Keyboard Free 我要是会正经的做法 就有鬼了. 我的数学水平没那么高. 三个同心圆 三个动点 求围成三角形面积的期望. 不会告辞. 其实可以\(n^2\)枚举角度然后算出面积 近 ...

  3. 2020牛客暑期多校训练营 第二场 J Just Shuffle 置换 群论

    LINK:Just Shuffle 比较怂群论 因为没怎么学过 置换也是刚理解. 这道题是 已知一个置换\(A\)求一个置换P 两个置换的关键为\(P^k=A\) 且k是一个大质数. 做法是李指导教我 ...

  4. 2020牛客暑期多校训练营 第二场 I Interval 最大流 最小割 平面图对偶图转最短路

    LINK:Interval 赛时连题目都没看. 观察n的范围不大不小 而且建图明显 考虑跑最大流最小割. 图有点稠密dinic不太行. 一个常见的trick就是对偶图转最短路. 建图有点复杂 不过建完 ...

  5. 2020牛客暑期多校训练营 第二场 C Cover the Tree 构造 贪心

    LINK:Cover the Tree 最受挫的是这道题,以为很简单 当时什么都想不清楚. 先胡了一个树的直径乱搞的贪心 一直过不去.后来意识到这类似于最经典长链剖分优化贪心的做法 然后那个是求最大值 ...

  6. 2020牛客暑期多校训练营 第二场 B Boundary 计算几何 圆 已知三点求圆心

    LINK:Boundary 计算几何确实是弱项 因为好多东西都不太会求 没有到很精通的地步. 做法很多,先说官方题解 其实就是枚举一个点 P 然后可以发现 再枚举一个点 然后再判断有多少个点在圆上显然 ...

  7. 2020牛客暑期多校训练营 第二场 A All with Pairs 字符串hash KMP

    LINK:All with Pairs 那天下午打这个东西的时候状态极差 推这个东西都推了1个多小时 (比赛是中午考试的我很困 没睡觉直接开肝果然不爽 一开始看错匹配的位置了 以为是\(1-l\)和\ ...

  8. 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem

    题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3  4 2 3 4 输出:0 0 1 题解: 认真想一 ...

  9. 2020牛客暑假多校训练营 第二场 H Happy Triangle set 线段树 分类讨论

    LINK:Happy Triangle 这道题很容易. 容易想到 a+b<x a<x<b x<a<b 其中等于的情况在第一个和第三个之中判一下即可. 前面两个容易想到se ...

随机推荐

  1. 【Git】Found a swap file by the name ".git/.MERGE_MSG.swp"

    最近合并分支的时候总是遇到这个问题,导致合并之后还需要再提交一次--有点烦-- 解决方案: 在项目根目录(如/StudioProjects/demo/Leave)下,找到 .git/.MERGE_MS ...

  2. 【Python-Django模型迁移】用户数据库模型的迁移(对其他数据库迁移同样适用)!!!

    迁移用户模型类 1. 指定用户模型类 文档 思考:为什么Django默认用户模型类是User? 阅读源代码:'django.conf.global_settings’ AUTH_USER_MODEL ...

  3. 深入理解JVM-类加载器深入解析(2)

    深入理解JVM-类加载器深入解析(2) 加载:就是把二进制形式的java类型读入java虚拟机中 连接: 验证: 准备:为类变量分配内存,设置默认值.但是在到达初始化之前,类变量都没有初始化为真正的初 ...

  4. vue在窗口大小改变时强制刷新组件的方法

    mounted () { window.onresize = () => { return (() => { this.$forceUpdate(); })() } }

  5. Django配置MySQL数据库

    一.在settings.py中配置 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # 数据库引擎 'NAME': ' ...

  6. 剑指offer-链表

    1. 链表中环的入口节点 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 思路一:用哈希表存已经遍历过的节点,O(1)复杂度查找,如果再次遇到就是环入口 # -*- cod ...

  7. Oracle - SPM固定执行计划(一)

    一.前言 生产中偶尔会碰到一些sql,有多种执行计划,其中部分情况是统计信息过旧造成的,重新收集下统计信息就行了.但是有些时候重新收集统计信息也解决不了问题,而开发又在嗷嗷叫,没时间让你去慢慢分析原因 ...

  8. temperatureConversion1

    (原题:https://www.python123.io/student/courses/934/groups/8102/problems/programmings/6078) Solution: # ...

  9. 自定义Dialog---实现优美对话框

    PS:自定义dialog,一些系统的dialog已经不能满足开发人员的需求了,所以,我们需要自定义一个属于并且适合自己项目的对话框,无论是颜色还是功能需求上都是和自己的项目紧密相关的,一些系统的对话框 ...

  10. 报error:getNetworkFromStore for nid failed while trying to build sandbox for cleanup: network

    docker服务起不来.报error:getNetworkFromStore for nid failed while trying to build sandbox for cleanup: net ...