답안 #1085600

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1085600 2024-09-08T13:13:33 Z CrabCNH Kronican (COCI16_kronican) C++14
100 / 100
571 ms 8540 KB
#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);
    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 - 1);
                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);
            }
        }
    }
    int ans = dp[0];
    for (int mask = 1; mask < (1 << n); mask ++) {
        if (__builtin_popcount (mask) <= k) {
            ans = min(ans, dp[mask]);
        }
    }
    cout << ans;
}

Compilation message

kronican.cpp: In function 'void read()':
kronican.cpp:20:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
kronican.cpp:21:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 7 ms 8540 KB Output is correct
6 Correct 14 ms 8536 KB Output is correct
7 Correct 28 ms 8540 KB Output is correct
8 Correct 58 ms 8540 KB Output is correct
9 Correct 571 ms 8540 KB Output is correct
10 Correct 563 ms 8536 KB Output is correct