2019牛客暑期多校训练营(第二场)D Partition problem(第k大)

链接:https://ac.nowcoder.com/acm/contest/882/D
来源:牛客网
 

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

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 wiwi representing the weight of each vertex.
Following N lines each contains N characters eijeij. There's an edge between vertex i and vertex j if and only if eij="1"eij="1".

1≤N≤1001≤N≤100
1≤K≤1061≤K≤106
0≤wi≤1090≤wi≤109
eij∈"01"eij∈"01"
eii="0"eii="0"
eij=ejieij=eji

输出描述:

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

示例1

输入

复制

2 3
1 2
01
10

输出

复制

2

说明

An empty set is considered as a clique.

       求(其中每两个点都直接相邻的)集团中第k大的重量是多少~~

       这里的话因为n其实不大~就可以放心搜。创一个结构体,用bitset来表示此刻点的取用状态,w来表示此时的总重量。优先队列存取,每次取可以取得的重量最小的状态,然后进行拓展。其中注意~因为要小心(1-2-3)相互连接这种情况时,一旦没有特殊的处理,很容易形成错误的答案::开始有1 ,然后同时拓展到1,2;1,3;再之后接着就会有两个1,2,3集团出现。所以我们规定每次添加点都从最后一个的后一个开始找能保证状态不重复了~~

 

#include
using namespace std;
int n, k; int w[105];
bitset<105>mp[105];
struct fuck {
	long long w;
	bitset<105>sta;
	bool operator < (const fuck a)const {
		return this->w > a.w;
	}
};
int main() {
	cin >> n >> k;
	for (int i = 0; i < n; i++) cin >> w[i];
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			char t; cin >> t;
			if (t == '1')mp[i][j] = '1';
		}
	}
	priority_queueq;
	bitset<105>tmp; tmp.reset();
	q.push(fuck{ 0,tmp });
	while (!q.empty()) {
		fuck now = q.top();
		q.pop();  k--;
		if (k == 0) {
			cout << now.w << "\n";
			return 0;
		}
		//	cout << now.sta << "\n";
		int j = 0;
		for (int i = 0; i < n; i++)
			if (now.sta[i])
				j = i + 1;
		for (int i = j; i < n; i++) {
			if (now.sta[i] == '1')continue;
			bitset<105>ne = now.sta&mp[i];
			if (ne == now.sta) {
				bitset<105>nex = now.sta;
				nex[i] = 1;
				q.push(fuck{ now.w + w[i],nex });
			}
		}
	}
	cout << "-1\n";
	return 0;
}

 

你可能感兴趣的:(简单搜索)