제출 #248715

#제출 시각아이디문제언어결과실행 시간메모리
248715Vladikus004Kronican (COCI16_kronican)C++14
100 / 100
1059 ms8800 KiB
#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]); } } } bool cmp(int x, int y){ return __builtin_popcount(x) > __builtin_popcount(y); } 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]; } } vector <int> v; for (int i = 1; i < (1<<n); i++) v.push_back(i); sort(all(v), cmp); fill(dp, dp + (1<<n) + 3, inf); // memset(dp, inf, sizeof dp); dp[v[0]] = 0; for (auto u: v) get_dp(u); 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 timeMemoryGrader output
Fetching results...