結果

問題 No.3158 Collect Stamps
コンテスト
ユーザー GOTKAKO
提出日時 2026-08-28 22:13:49
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 44 ms / 2,000 ms
+ 481µs
コード長 864 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,256 ms
コンパイル使用メモリ 216,880 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2026-08-28 22:13:57
合計ジャッジ時間 3,921 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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(unsigned 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