結果

問題 No.1284 Flip Game
ユーザー 37zigen37zigen
提出日時 2020-09-08 14:50:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 1,220 bytes
コンパイル時間 2,516 ms
コンパイル使用メモリ 78,660 KB
実行使用メモリ 56,380 KB
最終ジャッジ日時 2024-05-07 04:11:22
合計ジャッジ時間 7,732 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
54,252 KB
testcase_01 AC 128 ms
53,988 KB
testcase_02 AC 136 ms
54,132 KB
testcase_03 AC 117 ms
53,048 KB
testcase_04 AC 127 ms
54,216 KB
testcase_05 AC 129 ms
53,980 KB
testcase_06 AC 116 ms
53,040 KB
testcase_07 AC 127 ms
53,952 KB
testcase_08 AC 128 ms
54,340 KB
testcase_09 AC 132 ms
54,160 KB
testcase_10 AC 128 ms
53,944 KB
testcase_11 AC 134 ms
54,240 KB
testcase_12 AC 137 ms
53,820 KB
testcase_13 AC 142 ms
53,916 KB
testcase_14 AC 138 ms
54,100 KB
testcase_15 AC 171 ms
54,316 KB
testcase_16 AC 167 ms
54,384 KB
testcase_17 AC 179 ms
54,320 KB
testcase_18 AC 179 ms
54,388 KB
testcase_19 AC 226 ms
56,296 KB
testcase_20 AC 193 ms
53,932 KB
testcase_21 AC 181 ms
54,380 KB
testcase_22 AC 185 ms
54,080 KB
testcase_23 AC 195 ms
56,380 KB
testcase_24 AC 209 ms
56,268 KB
testcase_25 AC 227 ms
56,212 KB
testcase_26 AC 209 ms
56,364 KB
testcase_27 AC 210 ms
56,080 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