結果

問題 No.2711 Connecting Lights
ユーザー blue_jamblue_jam
提出日時 2024-03-31 15:47:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 129 ms / 5,000 ms
コード長 3,379 bytes
コンパイル時間 4,239 ms
コンパイル使用メモリ 268,808 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-31 15:48:05
合計ジャッジ時間 6,365 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 4 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 3 ms
6,676 KB
testcase_09 AC 76 ms
6,676 KB
testcase_10 AC 52 ms
6,676 KB
testcase_11 AC 93 ms
6,676 KB
testcase_12 AC 24 ms
6,676 KB
testcase_13 AC 28 ms
6,676 KB
testcase_14 AC 4 ms
6,676 KB
testcase_15 AC 16 ms
6,676 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 4 ms
6,676 KB
testcase_18 AC 5 ms
6,676 KB
testcase_19 AC 74 ms
6,676 KB
testcase_20 AC 29 ms
6,676 KB
testcase_21 AC 4 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 4 ms
6,676 KB
testcase_24 AC 46 ms
6,676 KB
testcase_25 AC 58 ms
6,676 KB
testcase_26 AC 127 ms
6,676 KB
testcase_27 AC 115 ms
6,676 KB
testcase_28 AC 129 ms
6,676 KB
testcase_29 AC 110 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

using ll = long long;
using mint = modint998244353;

ll solveF(ll H, ll W, ll K) {
    if (W == 1) {
        return 1LL << H;
    }
    vector<vector<mint>> dp(W + 1, vector<mint>(1 << H, 0));
    dp[0][(1 << H) - 1] = 1LL;
    for (ll j = 0; j < W; j++) {
        for (ll s = 0; s < (1 << H); s++) {
            for (ll t = 0; t < (1 << H); t++) {
                ll u = s & t;
                ll cnt = __builtin_popcount(u);
                if (cnt >= K) {
                    dp[j + 1][t] += dp[j][s];
                }
            }
        }
    }
    mint ans = 0;
    for (ll s = 0; s < (1 << H); s++) {
        ans += dp[W][s];
    }
    return ans.val();
}

ll solveFNaive(ll H, ll W, ll K) {
    return 0;
}

constexpr long long INF = (1LL << 60);

// 辺
struct Edge
{
    int from;
    int to;
    ll cost;
};

// ベルマンフォード法 (1.2 負閉路の影響を受ける頂点を調べる)
// 負の閉路が存在する場合 true を返し, 負閉路の影響を受ける頂点は -INF にセットされる
// distances は頂点数と同じサイズ, 全要素 INF で初期化しておく
bool BellmanFord(const std::vector<Edge>& edges, std::vector<long long>& distances, int startIndex)
{
    distances[startIndex] = 0;

    for (size_t i = 0; i < distances.size(); ++i)
    {
        bool changed = false;

        // 各辺について
        for (const auto& edge : edges)
        {
            // (INF + cost) は INF なので処理しない
            if (distances[edge.from] == INF)
            {
                continue;
            }

            // to までの新しい距離
            const long long d = (distances[edge.from] + edge.cost);

            // d が現在の記録より小さければ更新
            if (d < distances[edge.to])
            {
                distances[edge.to] = d;

                changed = true;
            }
        }

        // どの頂点も更新されなかったら終了
        if (!changed)
        {
            return false;
        }
    }

    // 頂点数分だけさらに繰り返し, 負閉路の影響を受ける頂点に -INF を伝播
    for (size_t i = 0; i < distances.size(); ++i)
    {
        for (const auto& edge : edges)
        {
            if (distances[edge.from] == INF)
            {
                continue;
            }

            const long long d = (distances[edge.from] + edge.cost);

            if (d < distances[edge.to])
            {
                // 負閉路の影響を受ける頂点を -INF に
                distances[edge.to] = -INF;
            }
        }
    }

    return true;
}

void solveE() {
    ll N, M;
    cin >> N >> M;
    vector<ll> A(N);
    for (ll i = 0; i < N; i++) {
        cin >> A[i];
    }

    vector<Edge> edges(M);
    for (ll i = 0; i < M; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        edges[i] = {(int)a, (int)b, c - A[b]};
    }

    vector<ll> dist;
    bool negativeRoop = BellmanFord(edges, dist, 0);

    if (dist[N - 1] == -INF) {
        cout << "inf" << endl;
    } else {
        cout << A[0] - dist[N - 1] << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);

    ll H, W, K;
    cin >> H >> W >> K;
    cout << solveF(H, W, K) << endl;

    return 0;
}
0