結果

問題 No.1284 Flip Game
ユーザー 37zigen37zigen
提出日時 2020-09-08 14:50:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 1,220 bytes
コンパイル時間 2,889 ms
コンパイル使用メモリ 74,484 KB
実行使用メモリ 58,400 KB
最終ジャッジ日時 2023-08-19 21:09:56
合計ジャッジ時間 8,917 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,768 KB
testcase_01 AC 118 ms
55,836 KB
testcase_02 AC 123 ms
55,680 KB
testcase_03 AC 116 ms
55,688 KB
testcase_04 AC 116 ms
55,380 KB
testcase_05 AC 115 ms
56,032 KB
testcase_06 AC 115 ms
55,708 KB
testcase_07 AC 117 ms
56,020 KB
testcase_08 AC 116 ms
55,944 KB
testcase_09 AC 117 ms
55,688 KB
testcase_10 AC 117 ms
55,468 KB
testcase_11 AC 121 ms
55,464 KB
testcase_12 AC 119 ms
55,768 KB
testcase_13 AC 124 ms
55,380 KB
testcase_14 AC 123 ms
55,680 KB
testcase_15 AC 148 ms
55,672 KB
testcase_16 AC 150 ms
56,224 KB
testcase_17 AC 171 ms
55,840 KB
testcase_18 AC 174 ms
55,620 KB
testcase_19 AC 194 ms
57,940 KB
testcase_20 AC 190 ms
56,000 KB
testcase_21 AC 189 ms
55,840 KB
testcase_22 AC 175 ms
56,192 KB
testcase_23 AC 207 ms
58,400 KB
testcase_24 AC 207 ms
56,464 KB
testcase_25 AC 207 ms
57,668 KB
testcase_26 AC 193 ms
57,812 KB
testcase_27 AC 194 ms
58,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	int[] p3=new int[15];
	{
		p3[0]=1;
		for (int i=1;i<p3.length;++i) p3[i]=3*p3[i-1];
	}
	
	void run() {
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		long[][] C=new long[N][N];
		for (int i=0;i<N;++i) {
			for (int j=0;j<N;++j) {
				C[i][j]=sc.nextLong();
			}
		}
		// 赤, 選ばれたことなし   0
		// 赤, 選ばれたことあり   1
		// 白,             2
		// 全て白にしたい
		long INF=Long.MAX_VALUE/3;
		long[][] dp=new long[p3[N]][N];
		for (int i=0;i<dp.length;++i)
			for (int j=0;j<dp[i].length;++j)
				dp[i][j]=INF;
		for (int i=0;i<N;++i)
			dp[p3[i]][i]=0;
		long ans=INF;
		for (int s=0;s<p3[N];++s) {
			for (int src=0;src<N;++src) {
				for (int dst=0;dst<N;++dst) {
					if (dp[s][src]==INF || s/p3[dst]%3==2 || src==dst) continue;
					int ns=s+p3[dst];
					dp[ns][dst]=Math.min(dp[ns][dst], dp[s][src]+C[src][dst]);
					if (ns==p3[N]-1 || ns+p3[dst]==p3[N]-1) {
						ans=Math.min(ans, dp[ns][dst]);
					}
				}
			}
		}
		System.out.println(ans);
	}
	
	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}
0