#include<iostream>
#include<vector>
using namespace std;
int root(int x,int parent[]){
if(x==parent[x]) return x;
parent[x]=root(parent[x],parent);
return parent[x];
}
void nadji(vector<vector<int> > *b,vector<int> *p,int k,int br,int n){
if((*p).size()==n){(*b).push_back(*p);return;}
if(br<k){
(*p).push_back(1);
nadji(b,p,k,br+1,n);
(*p).pop_back();
}
(*p).push_back(0);
nadji(b,p,k,br,n);
(*p).pop_back();
}
int izracunaj(int a){
if(a<=10) return a;
return 20-a;
}
int main(){
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
int n,k;
cin>>n>>k;
int parent[n];
int graf [n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>graf[i][j];
}
parent[i]=i;
}
int skupine=n;
long long best=999999999;
long long sol=0;
vector<vector<int> > bitmaske;
vector<int> priv;
nadji(&bitmaske,&priv,izracunaj(k),0,n);
for(int br=0;br<bitmaske.size();br++){
skupine=n;
for(int i=0;i<n;i++) parent[i]=i;
while(skupine>k){
int myn=999999999;
int indx1=0;
int indx2=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j) continue;
if(root(i,parent)!=root(j,parent)){
if(graf[i][j]<myn && parent[i]==i && ((bitmaske[br][parent[j]]==1 && k<=10) || (bitmaske[br][parent[j]]==0 && k>10))){
myn=graf[i][j];
indx1=i;
indx2=j;
}
}
}
}
if(myn==999999999) { sol=myn;break;}
parent[indx1]=parent[indx2];
sol+=myn;
skupine--;
//cout<<myn<<endl;
}
//for(int i=0;i<n;i++) cout<<parent[i];
//cout<<endl<<sol<<endl;
if(sol<best) best=sol;
//cout<<sol<<endl;
sol=0;
}
cout<<best;
}
Compilation message
kronican.cpp: In function 'void nadji(std::vector<std::vector<int> >*, std::vector<int>*, int, int, int)':
kronican.cpp:10:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if((*p).size()==n){(*b).push_back(*p);return;}
~~~~~~~~~~~^~~
kronican.cpp: In function 'int main()':
kronican.cpp:43:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int br=0;br<bitmaske.size();br++){
~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
5 |
Correct |
27 ms |
632 KB |
Output is correct |
6 |
Correct |
33 ms |
504 KB |
Output is correct |
7 |
Correct |
212 ms |
1840 KB |
Output is correct |
8 |
Correct |
379 ms |
2608 KB |
Output is correct |
9 |
Incorrect |
64 ms |
632 KB |
Output isn't correct |
10 |
Correct |
1291 ms |
18848 KB |
Output is correct |