#include <bits/stdc++.h>
using namespace std;
const int maxn = 20;
int n, k;
int c[maxn][maxn];
int dp[1 << 20];
int dist[maxn];
int qt;
int solve(int mask)
{
if(__builtin_popcount(mask) >= n - k) return 0;
if(dp[mask] != -1) return dp[mask];
int ans = 1e9;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(i == j or (mask & (1 << i)) or (mask & (1 << j))) continue;
ans = min(ans, solve(mask | (1 << i)) + c[i][j]);
}
}
return dp[mask] = ans;
}
int main()
{
cin >> n >> k;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
cin >> c[i][j];
}
memset(dp, -1, sizeof(dp));
qt = min(n - k, 0);
cout << solve(0) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4352 KB |
Output is correct |
2 |
Correct |
6 ms |
4352 KB |
Output is correct |
3 |
Correct |
6 ms |
4352 KB |
Output is correct |
4 |
Correct |
6 ms |
4352 KB |
Output is correct |
5 |
Correct |
18 ms |
4352 KB |
Output is correct |
6 |
Correct |
56 ms |
4572 KB |
Output is correct |
7 |
Correct |
83 ms |
4472 KB |
Output is correct |
8 |
Correct |
186 ms |
4480 KB |
Output is correct |
9 |
Execution timed out |
2025 ms |
4352 KB |
Time limit exceeded |
10 |
Correct |
170 ms |
4480 KB |
Output is correct |