結果

問題 No.3158 Collect Stamps
ユーザー GOTKAKO
提出日時 2025-05-23 21:08:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 859 bytes
コンパイル時間 2,032 ms
コンパイル使用メモリ 201,396 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2025-05-23 21:08:53
合計ジャッジ時間 4,055 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

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<int> A(K);
    for(auto &a : A) cin >> a,a--;
    vector<vector<int>> dist(N,vector<int>(N));
    for(auto &h : dist) for(auto &w : h) cin >> w;
    
    int n2 = 1<<N;
    vector<vector<int>> dp(n2,vector<int>(N,1e9));
    for(int i=0; i<N; i++) dp.at(1<<i).at(i) = 0;
    for(int i=1; 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(l) = min(dp.at(i).at(l),dp.at(i-(1<<l)).at(k)+dist.at(k).at(l));
        }
    }
    
    int answer = 1e9;
    for(int i=0; i<n2; i++){
        if(__popcount(i) < M) continue;
        for(auto pos : A) answer = min(answer,dp.at(i).at(pos));
    }
    cout << answer << endl;
}
0