답안 #98491

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
98491 2019-02-23T19:03:54 Z pamaj Kronican (COCI16_kronican) C++14
100 / 100
1217 ms 4560 KB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 20;

#pragma GCC optimize ("Ofast")

int n, k;
int c[maxn][maxn];
int dp[1 << 20];

int solve(const int& mask)
{
	if(__builtin_popcount(mask) <= k) return 0;

	if(dp[mask] != -1) return dp[mask];

	int ans = 1e7;

	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(i == j or !(mask & (1 << i)) or !(mask & (1 << j))) continue;

			ans = min(ans, solve(mask ^ (1 << i)) + c[i][j]);
		}
	}

	return dp[mask] = ans;
}


int32_t main()
{	
	ios::sync_with_stdio(false), cin.tie(nullptr);
	cin >> n >> k;

	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
			cin >> c[i][j];
	}


	for(int mask = 0; mask < (1 << n); mask++)
	{
		if(__builtin_popcount(mask) <= k) {dp[mask] = 0; continue;}

		int ans = 1e7;

		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < n; j++)
			{
				if(i == j or !(mask & (1 << i)) or !(mask & (1 << j))) continue;

				ans = min(ans, dp[mask ^ (1 << i)] + c[i][j]);
			}
		}

		dp[mask] = ans;
	}

	cout << dp[(1 << n) - 1] << "\n";
	//memset(dp, -1, sizeof(dp));

	//cout << solve((1 << n) - 1) << "\n";

}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 11 ms 404 KB Output is correct
6 Correct 26 ms 512 KB Output is correct
7 Correct 48 ms 632 KB Output is correct
8 Correct 102 ms 864 KB Output is correct
9 Correct 1217 ms 4560 KB Output is correct
10 Correct 89 ms 4472 KB Output is correct