#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int N = 21;
int n, k, a[N][N], dp[1 << N];
int Solve(int mask)
{
if (__builtin_popcount(mask) == k)
return 0;
int &res = dp[mask];
if (res != -1)
return res;
res = 1e9;
vector<int> now;
for (int i = 0; i < n; i++)
if (mask & (1 << i))
now.push_back(i);
for (int i = 0; i < now.size(); i++)
for (int j = 0; j < now.size(); j++)
if (i != j)
res = min(res, Solve(mask ^ (1 << now[i])) + a[now[i]][now[j]]);
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen ("file.inp","r",stdin);
// freopen ("file.out","w",stdout);
cin >> n >> k;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> a[i][j];
for (int i = 0; i < (1 << n); i++)
dp[i] = -1;
cout << Solve((1 << n) - 1);
return 0;
}
Compilation message
kronican.cpp: In function 'int Solve(int)':
kronican.cpp:18:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for (int i = 0; i < now.size(); i++)
| ~~^~~~~~~~~~~~
kronican.cpp:19:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for (int j = 0; j < now.size(); j++)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
348 KB |
Output is correct |
6 |
Correct |
24 ms |
348 KB |
Output is correct |
7 |
Correct |
50 ms |
2676 KB |
Output is correct |
8 |
Correct |
72 ms |
2648 KB |
Output is correct |
9 |
Correct |
735 ms |
4700 KB |
Output is correct |
10 |
Correct |
79 ms |
4696 KB |
Output is correct |