結果

問題 No.1284 Flip Game
ユーザー 37zigen
提出日時 2020-09-08 14:50:08
言語 Java
(openjdk 23)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 1,220 bytes
コンパイル時間 4,065 ms
コンパイル使用メモリ 77,640 KB
実行使用メモリ 43,572 KB
最終ジャッジ日時 2024-11-29 16:56:43
合計ジャッジ時間 9,198 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

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