結果

問題 No.1284 Flip Game
ユーザー kimiyukikimiyuki
提出日時 2020-11-06 22:04:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 2,057 bytes
コンパイル時間 2,254 ms
コンパイル使用メモリ 214,992 KB
実行使用メモリ 5,584 KB
最終ジャッジ日時 2023-09-29 18:45:53
合計ジャッジ時間 3,865 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 10 ms
4,376 KB
testcase_18 AC 10 ms
4,380 KB
testcase_19 AC 31 ms
5,040 KB
testcase_20 AC 12 ms
4,380 KB
testcase_21 AC 14 ms
4,376 KB
testcase_22 AC 14 ms
4,376 KB
testcase_23 AC 53 ms
5,552 KB
testcase_24 AC 48 ms
5,568 KB
testcase_25 AC 46 ms
5,548 KB
testcase_26 AC 32 ms
5,064 KB
testcase_27 AC 40 ms
5,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;
template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T> >;

int64_t solve(int n, const vector<vector<int64_t> >& c) {
    vector<int> three(n + 1);
    three[0] = 1;
    REP (i, n) {
        three[i + 1] = 3 * three[i];
    }

    // dijkstra
    vector<vector<int64_t> > dist(n, vector<int64_t>(three[n], INT64_MAX));
    reversed_priority_queue<tuple<int64_t, int, int> > que;
    REP (p, n) {
        dist[p][three[p]] = 0;
        que.emplace(0, p, three[p]);
    }
    while (not que.empty()) {
        int64_t dist_q_s; int q, s; tie(dist_q_s, q, s) = que.top();
        que.pop();
        if (dist[q][s] < dist_q_s) continue;

        vector<int> cur(n);
        {
            int t = s;
            REP (p, n) {
                cur[p] = t % 3;
                t /= 3;
            }
        }
        REP (p, n) if (cur[p] != 2 and p != q) {
            int t = s + three[p];
            if (dist[q][s] + c[q][p] < dist[p][t]) {
                dist[p][t] = dist[q][s] + c[q][p];
                que.emplace(dist[p][t], p, t);
            }
        }
    }

    int64_t ans = INT64_MAX;
    REP (p, n) {
        ans = min(ans, dist[p][three[n] - 1]);
        ans = min(ans, dist[p][three[n] - 1 - three[p]]);
    }
    return ans;
}

// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    constexpr char endl = '\n';
    int N;
    cin >> N;
    vector<vector<int64_t> > c(N, vector<int64_t>(N));
    REP (j, N) {
        REP (i, N) { cin >> c[j][i]; }
    }
    auto ans = solve(N, c);
    cout << ans << endl;
    return 0;
}
0