답안 #725512

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
725512 2023-04-17T14:38:43 Z vjudge1 Kronican (COCI16_kronican) C++17
90 / 100
2000 ms 8532 KB
#include <bits/stdc++.h>
using namespace std;

const int MN=21;
const int inf=1e9+7;

int n,k;
int board[MN][MN];
int dp[1<<MN];

int back(int bit){
//	cout<<bit<<endl;
	if(__builtin_popcount(bit)==k) return 0;
	if(dp[bit]>0) return dp[bit];
	
	int ans=inf;
	for(int i=1;i<=n;++i){
		if(!(bit&(1<<(i-1)))) continue;
		for(int j=1;j<=n;++j){
			if(i==j) continue;
			if(!(bit&(1<<(j-1)))) continue;
			
			ans=min(ans,board[j][i]+back(bit-(1<<(j-1))));
			ans=min(ans,board[i][j]+back(bit-(1<<(i-1))));
//			cout<<ans<<endl;
		}
	}
	return dp[bit]=ans;
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	
	memset(dp,-1,sizeof(dp));
	cin>>n>>k;
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n;++j){
			cin>>board[i][j];
		}
	}
	
	cout<<back((1<<n)-1);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8532 KB Output is correct
2 Correct 4 ms 8532 KB Output is correct
3 Correct 3 ms 8532 KB Output is correct
4 Correct 5 ms 8404 KB Output is correct
5 Correct 13 ms 8528 KB Output is correct
6 Correct 28 ms 8436 KB Output is correct
7 Correct 71 ms 8504 KB Output is correct
8 Correct 104 ms 8428 KB Output is correct
9 Correct 1268 ms 8512 KB Output is correct
10 Execution timed out 2086 ms 8404 KB Time limit exceeded