結果

問題 No.95 Alice and Graph
ユーザー maine_honzukimaine_honzuki
提出日時 2020-05-13 18:50:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,275 ms / 5,000 ms
コード長 1,719 bytes
コンパイル時間 1,577 ms
コンパイル使用メモリ 173,196 KB
実行使用メモリ 7,732 KB
最終ジャッジ日時 2023-10-12 17:01:51
合計ジャッジ時間 6,158 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 368 ms
7,620 KB
testcase_01 AC 55 ms
7,464 KB
testcase_02 AC 55 ms
7,548 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 564 ms
7,512 KB
testcase_05 AC 437 ms
7,732 KB
testcase_06 AC 555 ms
7,456 KB
testcase_07 AC 55 ms
7,656 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 1,275 ms
7,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int dp[1 << 16][16];

int main() {
    int N, M, K;
    cin >> N >> M >> K;
    vector<vector<int>> d(N, vector<int>(N, 100));
    for (int i = 0; i < M; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        d[a][b] = 1;
        d[b][a] = 1;
    }

    for (int k = 0; k < N; k++) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }

    vector<int> use{0};
    for (int i = N - 1; i > 0; i--) {
        vector<int> tmp = use;
        tmp.push_back(i);
        bool flag = false;

        for (int bit = 0; bit < 1 << (int)tmp.size(); bit++) {
            for (int j = 0; j < (int)tmp.size(); j++) {
                dp[bit][j] = 100;
            }
        }
        dp[1][0] = 0;

        for (int bit = 0; bit < 1 << (int)tmp.size(); bit++) {
            for (int x = 0; x < (int)tmp.size(); x++) {
                if (bit & 1 << x) {
                    for (int y = 0; y < (int)tmp.size(); y++) {
                        if (bit & 1 << y) {
                            dp[bit][x] = min(dp[bit][x], dp[bit ^ (1 << x)][y] + d[tmp[x]][tmp[y]]);
                        }
                    }
                }
            }
        }
        for (int j = 0; j < (int)tmp.size(); j++) {
            if (dp[(1 << (int)tmp.size()) - 1][j] <= K)
                flag = true;
        }

        if (flag) {
            use = tmp;
        }
        if ((int)use.size() == K + 1) {
            break;
        }
    }

    long long ans = 0;
    for (auto& x : use) {
        ans += (1ll << x) - 1;
    }
    cout << ans << endl;
}
0