#include<bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define ii pair<int, int>
#define iii pair<ii, int>
#define ll long long
#define pb push_back
#define pli pair<ll, int>
#define all(v) (v).begin(), (v).end()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define cntbit(mask) __builtin_popcount(mask)
#define getbit(x, i) ((x >> i) & 1)
#define MASK(i) (1 << i)
#define pli pair<ll, int>
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return 1;} return 0;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return 1;} return 0;}
const int N = 1e2 + 5;
const int V = 1e4 + 5;
const int MOD = 111539786;
int n, a[N][N], k, dp[MASK(20)];
void add(int &x, int y) {
x += y;
if (x >= MOD) x -= MOD;
}
void solve() {
cin >> n >> k;
for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < n ; j++) cin >> a[i][j];
memset(dp, 0x3f, sizeof dp);
dp[MASK(n) - 1] = 0;
for(int mask = MASK(n) - 1 ; mask >= 0 ; mask--) {
for(int i = 0 ; i < n ; i++)
if (getbit(mask, i))
for(int j = 0 ; j < n ; j++)
if (getbit(mask, j) && i != j)
minimize(dp[mask ^ MASK(i)], dp[mask] + a[i][j]);
}
int ans = 1e9;
for(int mask = 1 ; mask < MASK(n) ; mask++)
if (cntbit(mask) <= k) minimize(ans, dp[mask]);
cout << ans;
}
int main(void) {
//freopen("task.inp", "r", stdin);
//freopen("task.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1; while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
2 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
3 ms |
4432 KB |
Output is correct |
5 |
Correct |
8 ms |
4432 KB |
Output is correct |
6 |
Correct |
14 ms |
4568 KB |
Output is correct |
7 |
Correct |
29 ms |
4432 KB |
Output is correct |
8 |
Correct |
61 ms |
4604 KB |
Output is correct |
9 |
Correct |
579 ms |
4432 KB |
Output is correct |
10 |
Correct |
570 ms |
4432 KB |
Output is correct |