# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1085591 | CrabCNH | Kronican (COCI16_kronican) | C++14 | 53 ms | 17244 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define pii pair <int, int>
#define fi first
#define se second
using namespace std;
const int N = 20 + 5;
const int inf = 1e18 + 7;
int n, k;
int c[N][N];
int dp[1 << 20];
void read () {
#define task "crab"
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
}
bool bit (int x, int i) {
return ((x >> i) & 1);
}
int flip (int x, int i) {
return (x | (1 << i));
}
signed main () {
ios_base :: sync_with_stdio (0);
cin.tie (0);
read ();
cin >> n >> k;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
cin >> c[i][j];
}
}
memset(dp, 0x3f, sizeof dp);
int ans = dp[0];
dp[(1 << n) - 1] = 0;
for (int x = (1 << n) - 1; x >= 0; x --) {
for (int i = 1; i <= n; i ++) {
if (bit(x, i - 1)) {
int y = flip (x, i);
int cost = inf;
for (int j = 1; j <= n; j ++) {
if (bit (y, j - 1)) {
cost = min(cost, c[i][j]);
}
}
dp[y] = min(dp[y], dp[x] + cost);
}
}
}
for (int mask = 1; mask < (1 << n); mask ++) {
if (__builtin_popcount(mask) <= k) ans = min(ans, dp[mask]);
}
cout << ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |