2019 cattle off more school (second field) D.Kth Minimum Clique

Topic links: https://ac.nowcoder.com/acm/contest/882/D

We consider the start enumeration plus point is an empty set, then find the first $ k $ small, we only need a priority queue to maintain the current state of the right point, we find that $ n \ leq 100 $ $ k \ leq 10 ^ 6 $ so we just need to enumerate up to $ 10 ^ 6 $ states, and we want to ensure that the state will not be repeated to enumerate, every time we marked the largest point from the current state to begin the enumeration. The method of handling this type of set may be optimized by 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;
}

Guess you like

Origin www.cnblogs.com/inctry/p/11261469.html