#include <bits/stdc++.h>
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
const int N = 22;
int n, k, a[N][N], dp[(1<<N)];
set <pair <int, int> > ms;
void get_dp(int mask){
for (int i = 0; i < n; i++){
if (!((1<<i) & mask)) continue;
for (int j = 0; j < n; j++){
if ((!((1<<j) & mask)) || i == j) continue;
dp[mask ^ (1<<i)] = min(dp[mask ^ (1<<i)], dp[mask] + a[i][j]);
}
}
if (__builtin_popcount(mask) == k) return;
for (int i = 0; i < n; i++){
if (!((1<<i) & mask)) continue;
get_dp(mask ^ (1<<i));
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
cin >> n >> k;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cin >> a[i][j];
}
}
fill(dp, dp + (1<<n) + 3, inf);
// memset(dp, inf, sizeof dp);
dp[(1<<n)-1] = 0;
get_dp((1<<n) - 1);
int ans = inf;
// cout << dp[27] << "!\n";
for (int i = 1; i <= (1<<n); i++){
if (__builtin_popcount(i)==k) {
ans = min(ans, dp[i]);
// if (dp[i] == 4) cout << i << "!\n";
}
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
9 ms |
384 KB |
Output is correct |
4 |
Correct |
421 ms |
384 KB |
Output is correct |
5 |
Execution timed out |
2085 ms |
384 KB |
Time limit exceeded |
6 |
Execution timed out |
2098 ms |
512 KB |
Time limit exceeded |
7 |
Execution timed out |
2097 ms |
640 KB |
Time limit exceeded |
8 |
Execution timed out |
2077 ms |
896 KB |
Time limit exceeded |
9 |
Execution timed out |
2092 ms |
4480 KB |
Time limit exceeded |
10 |
Execution timed out |
2092 ms |
4480 KB |
Time limit exceeded |