#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> edge;
int dp[20][20];
bool seen[20];
void solve() {
int n, k;
cin >> n >> k;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> dp[i][j];
}
}
int ret = 1 << 30;
for(int mask = (1<<k)-1; mask < (1<<n); mask++) {
if(__builtin_popcount(mask) != k) continue;
int need = n;
priority_queue<edge> q;
memset(seen, 0, sizeof(seen));
{
int t = mask;
while(t) {
int curr = __builtin_ctz(t);
t &= t-1;
seen[curr] = true;
need--;
for(int i = 0; i < n; i++) {
q.push({-dp[i][curr], i});
}
}
}
int now = 0;
while(need && now < ret) {
edge curr = q.top();
q.pop();
if(seen[curr.second]) continue;
seen[curr.second] = true;
now -= curr.first;
need--;
for(int i = 0; i < n; i++) {
q.push({-dp[i][curr.second], i});
}
}
ret = min(ret, now);
}
cout << ret << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
508 KB |
Output is correct |
3 |
Incorrect |
2 ms |
576 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
576 KB |
Output isn't correct |
5 |
Incorrect |
16 ms |
644 KB |
Output isn't correct |
6 |
Incorrect |
12 ms |
648 KB |
Output isn't correct |
7 |
Incorrect |
66 ms |
652 KB |
Output isn't correct |
8 |
Incorrect |
110 ms |
668 KB |
Output isn't correct |
9 |
Incorrect |
23 ms |
668 KB |
Output isn't correct |
10 |
Correct |
848 ms |
668 KB |
Output is correct |