結果
問題 |
No.1284 Flip Game
|
ユーザー |
|
提出日時 | 2020-09-08 14:58:05 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 229 ms / 2,000 ms |
コード長 | 1,319 bytes |
コンパイル時間 | 2,185 ms |
コンパイル使用メモリ | 79,688 KB |
実行使用メモリ | 43,628 KB |
最終ジャッジ日時 | 2024-11-29 17:26:54 |
合計ジャッジ時間 | 7,962 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 28 |
ソースコード
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(); assert(2<=N&&N<=9); 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(); if (i==j) assert(C[i][j]==0); else assert(1<=C[i][j]&&C[i][j]<=1e8); } } // 赤, 選ばれたことなし 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));} }