結果

問題 No.3158 Collect Stamps
ユーザー ha_chan
提出日時 2025-05-23 19:35:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,182 bytes
コンパイル時間 2,123 ms
コンパイル使用メモリ 219,660 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2025-05-23 19:35:49
合計ジャッジ時間 3,669 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<VL> VVL;
typedef long long LL;
#define all(a) (a).begin(), (a).end()
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define ALL(a)  (a).begin(),(a).end()
#define pb push_back

int p2[21];


int main() {
  p2[0]=1;
  rep(i,20){p2[i+1]=p2[i]*2;}
  int n,m,k;cin>>n>>m>>k;
  set<int> A; int a;
  rep(i,k){cin>>a; A.insert(a-1);}
  int T[n][n];
  rep(i,n)rep(j,n){cin>>T[i][j];}
  map<VI,int> rec;
  queue<VI> D;
  rep(i,n){rec[{p2[i],i,1}]=0; D.push({p2[i],i,1});}
  int ans=-1;
  while(D.size()>0){
    VI z=D.front();D.pop();
    //cout<<z[2]<<endl;
    int kk=rec[z];
    if(z[2]==m){
      if(A.count(z[1])){
        if(ans==-1){ans=kk;}
        else{ans=min(ans,kk);}
      }
      continue;
    }
    rep(i,n){
      if((p2[i]&z[0])==0){
        VI zz={z[0]+p2[i],i,z[2]+1};
        if(rec.count(zz)){rec[zz]=min(rec[zz],kk+T[z[1]][i]);}
        else{rec[zz]=kk+T[z[1]][i];D.push(zz);}
      }
    }
  }  
  cout<<ans<<endl;
}
0