2019牛客多校(第二场)D.Kth Minimum Clique

题目链接:https://ac.nowcoder.com/acm/contest/882/D

我们考虑为空集时开始枚举加点,那么求第$k$小,我们只需要用优先队列来维护当前状态的点权,我们发现$n\leq 100$ $k\leq 10^6$那么,我们至多只需要枚举$10^6$个状态,同时我们要保证状态不会被重复枚举到,我们每次从当前状态下标最大的点开始枚举即可。处理这一类集合的方法可以用bitset来优化

#include <bits/stdc++.h>
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define lowbit(x) ((x)&(-x))
#define mem(i, a) memset(i, a, sizeof(i))
#define sqr(x) ((x)*(x))
#define all(x) x.begin(),x.end()
#define ls (k << 1)
#define rs (k << 1 | 1)
using namespace std;
typedef long long ll;
template <typename T>
inline void read(T &X) {
    X = 0; char ch = 0; T op = 1;
    for(; ch > '9' || ch < '0'; ch = getchar())
        if(ch == '-') op = -1;
    for(; ch >= '0' && ch <= '9'; ch = getchar())
        X = (X << 3) + (X << 1) + ch - 48;
    X *= op;
}

const int INF = 0x3f3f3f3f;
const int N = 105 + 5;
struct node {
    ll w,s;
    bitset<105> cli;
    bool operator <(const node &a) const {
        return w > a.w;
    }
};
int n,k,val[N];
bitset<105> e[N];
void bfs() {
    priority_queue<node> q;
    bitset<105> fir;
    fir.reset();
    q.push({0, 0, fir});
    int cnt = 0;
    while(!q.empty()) {
        node u = q.top(); q.pop();
       // cout << u.w << "\n";
        cnt++; if(cnt == k) {cout << u.w << "\n"; exit(0);}
        for(int i = u.s + 1; i <= n; i++) {
            if((e[i] & u.cli) == u.cli) {
                bitset<105> b(u.cli);
                b.set(i, 1);
                q.push({u.w + val[i], i, b});
            }
        }
    }
    cout << -1;
}
int main() {
#ifdef INCTRY
    freopen("input.txt", "rt", stdin);
#endif
    read(n); read(k);
    for(int i = 1; i <= n; i++) read(val[i]);
    for(int i = 1; i <= n; i++) {
        string s; cin >> s;
       // cout << s;
        for(int j = 1; j <= n; j++) {
           // cout << s[j - 1];
            if(s[j - 1] == '1') e[i].set(j, 1);
            else e[i].set(j, 0);
        }
    }
    bfs();


#ifdef INCTRY
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/inctry/p/11261469.html