結果

問題 No.95 Alice and Graph
ユーザー GOTKAKO
提出日時 2025-07-01 05:15:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,487 ms / 5,000 ms
コード長 1,227 bytes
コンパイル時間 2,167 ms
コンパイル使用メモリ 204,604 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-07-01 05:15:56
合計ジャッジ時間 7,572 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,M,K; cin >> N >> M >> K;
    vector<vector<int>> dist(N,vector<int>(N,K+1));
    for(int i=0; i<N; i++) dist.at(i).at(i) = 0;
    while(M--){
        int a,b; cin >> a >> b; a--,b--;
        dist.at(a).at(b) = 1,dist.at(b).at(a) = 1;
    }
    for(int i=0; i<N; i++) for(int k=0; k<N; k++) for(int l=0; l<N; l++) dist.at(k).at(l) = min(dist.at(k).at(l),dist.at(k).at(i)+dist.at(i).at(l));

    vector<int> P;
    long long answer = 0;
    for(int d=N-1; d>0; d--){
        if(P.size() == K) break;
        P.push_back(d);
        int n = P.size(),n2 = 1<<n;;
        vector<vector<int>> dp(n2,vector<int>(n,K+1));
        for(int i=0; i<n; i++) dp.at(1<<i).at(i) = dist.at(0).at(P.at(i));
        for(int i=3; i<n2; i++){
            for(int k=0; k<n; k++) if(i&(1<<k)) for(int l=0; l<n; l++) if(k != l && (i&(1<<l))){
                dp.at(i).at(k) = min(dp.at(i).at(k),dp.at(i-(1<<k)).at(l)+dist.at(P.at(l)).at(P.at(k)));
            }
        }
        if(*min_element(dp.back().begin(),dp.back().end()) <= K) answer += (1LL<<d)-1;
        else P.pop_back();
    }
    cout << answer << endl;
}
0