#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
int val[25][25];
vector<pair<int, pii>> v;
int n, k;
int prt[25];
int find(int x){
if(prt[x] == x)
return x;
return prt[x] = find(prt[x]);
}
int merge(int x, int y){
x = find(x);
y = find(y);
prt[x] = y;
}
int bitcnt(int x){
int ret = 0;
while(x > 0){
ret += x%2;
x /= 2;
}
return ret;
}
int f(int bit){
int ret = 0;
for(int i = 0; i < n; ++i)
prt[i] = i;
int root = -1;
for(int i = 0; i < n; ++i){
if((bit&(1<<i)) == 0)
continue;
if(root == -1)
root = i;
else
merge(root, i);
}
for(auto u : v){
if(find(u.ss.ff) != find(u.ss.ss)){
ret += u.ff;
merge(u.ss.ff, u.ss.ss);
}
}
return ret;
}
int sfunc(pair<int, pii> a, pair<int, pii> b){return a.ff < b.ff;}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n >> k;
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j){
cin >> val[i][j];
if(i != j)
v.pb({val[i][j], {i, j}});
}
sort(v.begin(), v.end(), sfunc);
int ans = 1e9;
for(int i = 0; i < (1<<n); ++i){
if(bitcnt(i) == k){
ans = min(ans, f(i));
//cout << i << " " << f(i) << "\n";
}
}
cout << ans << "\n";
}
Compilation message
kronican.cpp: In function 'int merge(int, int)':
kronican.cpp:27:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
5 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
6 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
7 |
Incorrect |
19 ms |
376 KB |
Output isn't correct |
8 |
Incorrect |
32 ms |
504 KB |
Output isn't correct |
9 |
Incorrect |
18 ms |
376 KB |
Output isn't correct |
10 |
Incorrect |
256 ms |
504 KB |
Output isn't correct |