結果

問題 No.1284 Flip Game
ユーザー platinumplatinum
提出日時 2020-11-05 21:32:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 1,199 bytes
コンパイル時間 608 ms
コンパイル使用メモリ 70,656 KB
実行使用メモリ 4,948 KB
最終ジャッジ日時 2023-09-29 17:33:55
合計ジャッジ時間 2,739 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 14 ms
4,820 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 5 ms
4,376 KB
testcase_22 AC 5 ms
4,376 KB
testcase_23 AC 14 ms
4,864 KB
testcase_24 AC 13 ms
4,844 KB
testcase_25 AC 13 ms
4,920 KB
testcase_26 AC 13 ms
4,948 KB
testcase_27 AC 14 ms
4,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using namespace std;
using LL = long long;
const LL Max_C = 1e8;
const int Max_N = 1e5;
const LL INF = 1e12;

LL c[10][10];
LL dp[Max_N][10];

LL power(LL x, LL n){
	LL r = 1;
	while(n){
		if(n & 1) r = r * x;
		x = x * x;
		n >>= 1;
	}
	return r;	
}

int N, fin;

LL dfs(int S, int v){
	if(dp[S][v] != INF) return dp[S][v];
	int num = power(3, v);
	if(S / num % 3 == 0) return INF;
	LL res = INF;
	rep(i,N){
		if(S != fin - 1 and i == v) continue;
		int prev_num = power(3, i);
		if(S / prev_num % 3 == 0) continue;
		int pS = S - num;
		res = min(res, dfs(pS, i) + c[i][v]);
	}
	return dp[S][v] = res;
}

int main(){
  cin >> N;
  assert(2 <= N and N <= 9);
  rep(i,N){
  	rep(j,N){
  		LL cost;
  		cin >> cost;
  		if(i == j) assert(cost == 0);
  		else assert(1 <= cost and cost <= Max_C);
  		c[i][j] = cost;
  	}
  }
  fin = power(3, N);
  rep(i,fin){
  	rep(j,N) dp[i][j] = INF;
  }
  rep(i,N){
  	int n = power(3, i);
  	dp[n][i] = 0;
  }
  rep(i,N) dfs(fin - 1, i);
  LL ans = INF;
  rep(i,N) ans = min(ans, dp[fin - 1][i]);
  cout << ans << endl;

  return 0;
}
0