結果
問題 |
No.3158 Collect Stamps
|
ユーザー |
![]() |
提出日時 | 2025-05-24 12:17:28 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 51 ms / 2,000 ms |
コード長 | 1,562 bytes |
コンパイル時間 | 3,596 ms |
コンパイル使用メモリ | 283,644 KB |
実行使用メモリ | 13,952 KB |
最終ジャッジ日時 | 2025-05-24 12:17:34 |
合計ジャッジ時間 | 5,649 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
ソースコード
#ifndef ONLINE_JUDGE #define _GLIBCXX_DEBUG #endif #include<bits/stdc++.h> using namespace std; using ll = long long; //https://boostjp.github.io/tips/multiprec-int.html #define YES cout<<"Yes"<<endl #define NO cout<<"No"<<endl #define YN {cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}// if(a==b)YN; #define NO2 cout<<-1<<endl #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rrep(i, n) for (int i=int(n)-1; i>=0; --i) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() int pc(ll n) { int cnt = 0; while (n>0) { cnt += n%2; n/=2; } return cnt; } int main() { int N, M, K; cin >> N >> M >> K; vector<int> A(K); rep(i, K) { cin >> A[i]; A[i]--; } vector<vector<int>> T(N, vector<int>(N)); rep(i,N)rep(j,N) cin >> T[i][j]; //dp[i][j] = {visited, last} vector<vector<ll>> dp(1<<N, vector<ll>(N, ll(1e18))); rep(i, N) { dp[1<<i][i] = 0; } rep(i, (1<<N)) { rep(j, N) { if (((i>>j) & 1) == 0) continue; rep(k, N) { if (i+(1<<k) >= (1<<N)) continue; if ((i>>k) & 1) continue; //cerr << "now: " << i << " last: " << j << " next: " << k << ' ' << i+(1<<k) << endl; dp[i+(1<<k)][k] = min(dp[i+(1<<k)][k], dp[i][j] + T[j][k]); } } } ll ans = ll(1e18); rep(i, (1<<N)) { if (pc(i) < M) continue; for (int a:A) { ans = min(ans, dp[i][a]); } } cout << ans << endl; }