結果

問題 No.95 Alice and Graph
ユーザー ichyoichyo
提出日時 2014-12-07 21:00:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 301 ms / 5,000 ms
コード長 1,504 bytes
コンパイル時間 1,477 ms
コンパイル使用メモリ 150,108 KB
実行使用メモリ 5,388 KB
最終ジャッジ日時 2023-08-09 07:09:35
合計ジャッジ時間 3,779 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 224 ms
5,272 KB
testcase_01 AC 35 ms
5,348 KB
testcase_02 AC 35 ms
5,252 KB
testcase_03 AC 2 ms
4,512 KB
testcase_04 AC 301 ms
5,344 KB
testcase_05 AC 247 ms
5,276 KB
testcase_06 AC 291 ms
5,388 KB
testcase_07 AC 34 ms
5,252 KB
testcase_08 AC 2 ms
4,508 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 17 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
using namespace std;
typedef long long LL;
const int INF = 1e9;

int main(){
    int N, M, K;
    cin >> N >> M >> K;
    int d[60][60] = {};
    REP(i, N) REP(j, N) d[i][j] = (i == j ? 0 : INF);
    REP(i, M) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        d[a][b] = d[b][a] = 1;
    }

    REP(k, N) REP(i, N) REP(j, N) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    vector<int> seq;

    int dp[1 << 15][15];
    for(int k = N - 1; k > 0; k--) if(d[0][k] <= K) {
        if(seq.size() == K) break;
        vector<int> w = seq;
        w.push_back(k);
        int L = w.size();
        REP(s, 1 << L) REP(i, L) dp[s][i] = INF;
        REP(i, w.size()) {
            dp[1 << i][i] = d[0][w[i]];
        }
        REP(s, 1 << L) REP(i, L) {
            int u = w[i];
            REP(j, L) if(!(s >> j & 1)) {
                int v = w[j];
                int ns = s | (1 << j);
                if(d[u][v] == INF) continue;
                int nd = dp[s][i] + d[u][v];
                dp[ns][j] = min(dp[ns][j], nd);
            }
        }
        int check = K + 1;
        REP(i, L) check = min(check, dp[(1 << L) - 1][i]);
        if(check <= K) {
            seq = w;
        } else {
            continue;
        }
    }

    long long ans = 0;
    for(int k : seq) {
        ans += (1LL << k) - 1;
    }
    cout << ans << endl;

    return 0;
}
0