Kth Minimum Clique

题目描述

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.

输入描述:

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≤1e6
0≤wi≤1e9
eij∈"01"
eii="0"
eij=eji

输出描述:

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

输入

2 3
1 2
01
10

输出

2

说明

An empty set is considered as a clique.
题目链接:https://ac.nowcoder.com/acm/contest/882/D

题意:找到权值第K小的完全子图。
思路:考虑一开始我们所选择的子图是个空集,然后逐步向里面加点。这样加点的方式会导致每一次从当前状态,都会产生多个下一状态(比如当前是空集,我们可以把任意一个点加进去,就会有n种状态),那么若是我们可以找到一种方式,使得我们可以按照状态的权值递增的顺序来遍历这些状态,那么遍历到第K个状态时就是答案。于是我们可以用优先队列来实现这种遍历方式,即优先队列每次将当前已拓展的所有状态中,权值最小的那个拿来去拓展其他状态。但是还一个小问题就是我们要保证我们拓展的状态不能重复也不能遗漏,于是只要每次在当前状态的已选中的点中下标最大的点后面拓展,就可以保证不重复不遗漏了。
#include<bits/stdc++.h>
using namespace std;
int w[];
bitset<>Map[];
char M[][]; struct ss
{
bitset<>state;
long long w; bool operator < (const ss & s)const
{
return w>s.w;
}
}; priority_queue<ss>q;
int n,k;
long long spfa(ss now)
{
q.push(now); while(!q.empty())
{
now=q.top();
q.pop();
k--; if(!k)
return now.w; int pos=;
for(int i=;i<n;i++)if(now.state[i])pos=i+; for(int i=pos; i<n; i++)
{
if(now.state[i]==)
{
if((now.state&Map[i])==now.state)//O(1)拓展新状态
{
now.state[i]=;
now.w+=w[i];
q.push(now);
now.state[i]=;
now.w-=w[i];
}
}
}
}
return -;
} int main()
{
scanf("%d %d",&n,&k); for(int i=; i<n; i++)
scanf("%d",&w[i]);
for(int i=; i<n; i++)
scanf("%s",M[i]); for(int i=; i<n; i++)
for(int j=; j<n; j++)
if(M[i][j]=='')Map[i].set(j); ss now;
now.state.reset();
now.w=; printf("%lld\n",spfa(now));
return ;
}

Kth Minimum Clique的更多相关文章

  1. 牛客竞赛第二场D Kth Minimum Clique 贪心+bitmap

    Kth Minimum Clique 题意 给出n(n<100)个点的邻接表,和n个点的权值,求第k大的团(完全子图) 分析 n很小,并且好像没有什么算法和这个有关系,所以可以往暴力枚举的方向想 ...

  2. 牛客网多校训练第二场D Kth Minimum Clique

    链接:https://ac.nowcoder.com/acm/contest/882/D来源:牛客网 Given a vertex-weighted graph with N vertices, fi ...

  3. 第k小团(Bitset+bfs)牛客第二场 -- Kth Minimum Clique

    题意: 给你n个点的权值和连边的信息,问你第k小团的值是多少. 思路: 用bitset存信息,暴力跑一下就行了,因为满足树形结构,所以bfs+优先队列就ok了,其中记录下最后进入的点(以免重复跑). ...

  4. Kth Minimum Clique(2019年牛客多校第二场D题+k小团+bitset)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 找第\(k\)小团. 思路 用\(bitset\)来标记每个结点与哪些结点直接有边,然后进行\(bfs\),在判断新加入的点与现在有的点是否都 ...

  5. 【bitset】Kth Minimum Clique

    #include<bits/stdc++.h> #define B bitset<105> using namespace std; typedef long long ll ...

  6. 2019牛客暑期多校训练营(第二场)-D Kth Minimum Clique

    题目链接:https://ac.nowcoder.com/acm/contest/882/D 题意:求给定点权无向图中,点权和第k小的完全子图的点权和.(包括空集) 思路:从空集开始,每找到一个完全子 ...

  7. 2019牛客暑期多校训练营(第二场)D Kth Minimum Clique(第k团)

    题意:给你n个点 求第k小的团 思路:暴力bfs+bitset压位 #include <bits/stdc++.h> using namespace std; const int N = ...

  8. 2019牛客多校第二场D-Kth Minimum Clique

    Kth Minimum Clique 题目传送门 解题思路 我们可以从没有点开始,把点一个一个放进去,先把放入一个点的情况都存进按照权值排序的优先队列,每次在新出队的集合里增加一个新的点,为了避免重复 ...

  9. Kth Minimum Clique_2019牛客暑期多校训练营(第二场)

    题目连接: https://ac.nowcoder.com/acm/contest/882/D Description Given a vertex-weighted graph with N ver ...

随机推荐

  1. 关于H5裁剪图片后,直传阿里云的一些问题

    这段时间在工作中碰到一个需要在h5裁剪图像,然后直传阿里云的需求.图中遇到了一些小问题,分享出来大家都看看. h5裁剪图像:cropper.js是一个神器啊关于用法,网上可以收罗出大量的帖子,这里我就 ...

  2. IOS 检测摇晃 几个问题

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://kyoworkios.blog.51cto.com/878347/1152692 ...

  3. Neo4j与springdata集成

    1.maven工程需导入的jar包 <!-- neo4j --> <dependency> <groupId>org.springframework.data< ...

  4. Could not resolve placeholder 'CUST_INDUSTORY' in string value "${CUST_INDUSTORY}"

    问题描述 项目中的资源文件中写了个properties文件,内容这样的 CUST_FROM=002 CUST_INDUSTORY=001 CUST_LEVEL=006 在springmvc配置文件中加 ...

  5. Fiilter

    过滤器 过滤请求和响应 作用:        自动登录.        统一编码.        过滤关键字        .... Filter是一个接口 编写filter步骤: 1.编写一个类 a ...

  6. 全面解决Html页面缓存的问题

    页面缓存的问题可能大家都遇到过,很多功能做完没起效果,那么怎么解决这个问题呢?这里给出我的使用的解决方法 对于一个html页面,缓存分3部分,一个是页面内容,一个是css样式,一个是JS文件1.页面内 ...

  7. 二进制操作(1)–Bytes

    1,Bytes的单元被当作字符串处理. 例如: 有些介绍会声称上述程序会得到这样的结果:b'\x00\x00\x00\x00' 在python v2.7.10上是得不到此结果的. 实际上,如果 typ ...

  8. HZOI20190819模拟26题解

    题面:https://www.cnblogs.com/Juve/articles/11376806.html A. 嚎叫响彻在贪婪的厂房: 是时候学习一下map和set的用法了...... 贪心:区间 ...

  9. CODE[VS]4633:Mz树链剖分练习

    Description 给定一棵结点数为n的树,初始点权均为0,有依次q个操作,每次操作有三个参数a,b,c,当a=1时,表示给b号结点到c号结点路径上的所有点(包括b,c,下同)权值都增加1,当a= ...

  10. 新闻内页 上一篇写一篇问题,ID不连续,不用链表

    y要什么链表? 用sql查询上一篇 SELECT id,title FROM t_article WHERE id<10 ORDER BY id DESC LIMIT 1; 用sql查下一篇 S ...