結果

問題 No.1284 Flip Game
ユーザー platinumplatinum
提出日時 2020-09-03 22:28:23
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 898 bytes
コンパイル時間 675 ms
コンパイル使用メモリ 74,840 KB
実行使用メモリ 8,448 KB
最終ジャッジ日時 2024-11-26 10:37:46
合計ジャッジ時間 2,126 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define rep(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;
using LL = long long;
const int Max_N = 12;
const int Max_S = 1e5;
const int INF = 2e9+5;

int N, M, fin;
int c[Max_N][Max_N];
int dp[Max_S][Max_N];

int rec(int s, int v){
	if(dp[s][v]!=INF) return dp[s][v];
	int res=INF;
	rep(u,N){
		if(u==v and s!=fin) continue;
		int div=pow(M,u);
		if(s/div%M==0) continue;
		int ns=s-pow(M,v);
		res=min(res,rec(ns,u)+c[u][v]);
	}
	return dp[s][v]=res;
}

int main(){
	cin >> N;
	M=3;
	fin=pow(M,N)-1;
	rep(i,N){
		rep(j,N) cin >> c[i][j];
	}
	rep(i,Max_S){
		rep(j,Max_N) dp[i][j]=INF;
	}
	rep(i,N){
		int st=pow(3,i);
		dp[st][i]=0;
	}
	rep(i,N) rec(fin,i);
	int ans=INF;
	rep(i,N) ans=min(ans,dp[fin][i]);
	cout << ans << endl;

	return 0;
}
0