题意:
给你n个点的权值和连边的信息,问你第k小团的值是多少。
思路:
用bitset存信息,暴力跑一下就行了,因为满足树形结构,所以bfs+优先队列就ok了,其中记录下最后进入的点(以免重复跑)。
1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream> 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 23 #define mem(a,b) memset(a,b,sizeof(a)) 24 #define fo(a,b,c) for(a=b;a<=c;++a)//register int i 25 #define fr(a,b,c) for(a=b;a>=c;--a) 26 #define pr printf 27 #define sc scanf 28 void swapp(int &a,int &b); 29 double fabss(double a); 30 int maxx(int a,int b); 31 int minn(int a,int b); 32 int Del_bit_1(int n); 33 int lowbit(int n); 34 int abss(int a); 35 //const long long INF=(1LL<<60); 36 const double E=2.718281828; 37 const double PI=acos(-1.0); 38 const int inf=(1<<29); 39 const double ESP=1e-9; 40 const int mod=(int)1e9+7; 41 const int N=(int)1e6+6; 42 43 int val[105]; 44 struct node 45 { 46 int End; 47 long long V; 48 bitset<105> clique;//团 49 friend bool operator<(node a,node b) 50 { 51 return a.V>b.V; 52 } 53 }; 54 bitset<105> mp[105]; 55 priority_queue<node> q; 56 57 int main() 58 { 59 int n,k,i,j; 60 sc("%d%d",&n,&k); 61 for(i=1;i<=n;++i) 62 sc("%d",&val[i]); 63 for(i=1;i<=n;++i) 64 for(j=1;j<=n;++j) 65 { 66 int t; 67 sc("%1d",&t); 68 mp[i][j]=t; 69 } 70 bitset<105> temp; 71 q.push({0,0,temp}); 72 while(!q.empty()) 73 { 74 node now=q.top();q.pop(); 75 k--; 76 if(k==0) 77 { 78 pr("%lld ",now.V); 79 return 0; 80 } 81 for(i=now.End+1;i<=n;++i) 82 { 83 if((now.clique&mp[i])==now.clique) 84 { 85 node v=now; 86 v.clique[i]=1; 87 v.End=i; 88 v.V+=val[i]; 89 q.push(v); 90 } 91 } 92 } 93 pr("-1 "); 94 return 0; 95 } 96 97 /**************************************************************************************/ 98 99 int maxx(int a,int b) 100 { 101 return a>b?a:b; 102 } 103 104 void swapp(int &a,int &b) 105 { 106 a^=b^=a^=b; 107 } 108 109 int lowbit(int n) 110 { 111 return n&(-n); 112 } 113 114 int Del_bit_1(int n) 115 { 116 return n&(n-1); 117 } 118 119 int abss(int a) 120 { 121 return a>0?a:-a; 122 } 123 124 double fabss(double a) 125 { 126 return a>0?a:-a; 127 } 128 129 int minn(int a,int b) 130 { 131 return a<b?a:b; 132 }