More than 2019 cattle off summer school camp (second field) D bitset

The meaning of problems

N-node to a little right of FIG finding the k-th smaller weight groups

analysis

Groups represented by the bitset state, a node must have and where each node are connected edge to be added, it can be directly \ (\ & \) operation to determine whether the group is formed further a node can be added into weights used to maintain a priority queue of k small front group.

Code

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define ll long long
using namespace std;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=1e5+10;
int n,k,w[110];
typedef bitset<110> bit;
bit e[110];
struct ppo{
    bit x;
    ll val;
    bool operator <(const ppo &r)const{
        return val>r.val;
    }
};
bool cmp(int x,int y){
    return w[x]<w[y];
}
int main(){
    //ios::sync_with_stdio(false);
    //freopen("in","r",stdin);
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&w[i]);
    }
    for(int i=1;i<=n;i++){
        string s;
        cin>>s;
        for(int j=0;j<s.size();j++){
            e[i][j+1]=s[j]-'0';
        }
    }
    priority_queue<ppo>q;
    bit xxg;
    xxg.reset();
    q.push(ppo{xxg,0});
    vector<ll>v;
    while(!q.empty()){
        ppo u=q.top();
        v.pb(u.val);
        if(v.size()>k) break;
        q.pop();
        int z=1;
        for(int i=1;i<=n;i++){
            if(u.x[i]) z=i+1;
        }
        for(int i=z;i<=n;i++){
            if(u.x[i]==1) continue;
            bit ret=e[i]&u.x;
            if(ret==u.x){
                ret[i]=1;
                q.push(ppo{ret,u.val+w[i]});
            }
        }
    }
    sort(v.begin(), v.end());
    if(v.size()<k) puts("-1");
    else printf("%lld\n",v[k-1]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/xyq0220/p/11346530.html