結果
| 問題 |
No.1284 Flip Game
|
| コンテスト | |
| ユーザー |
platinum
|
| 提出日時 | 2020-11-05 21:32:07 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 14 ms / 2,000 ms |
| コード長 | 1,199 bytes |
| コンパイル時間 | 651 ms |
| コンパイル使用メモリ | 72,480 KB |
| 実行使用メモリ | 6,980 KB |
| 最終ジャッジ日時 | 2024-07-22 11:34:54 |
| 合計ジャッジ時間 | 1,594 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
#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;
}
platinum