結果

問題 No.3158 Collect Stamps
ユーザー 橋本ヒデヒコ
提出日時 2025-07-09 17:08:45
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 2,472 bytes
コンパイル時間 6,069 ms
コンパイル使用メモリ 148,964 KB
実行使用メモリ 16,396 KB
最終ジャッジ日時 2025-07-09 17:08:54
合計ジャッジ時間 6,259 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <iterator>


using namespace std;

using i64 = int64_t;
// using u64 = uint64_t;

template<class T>
using VV = vector<vector<T>>;

#define REP(i, n) for(i64 i = 0; i < i64(n); i++)
#define REP1(i, n) for(i64 i = 1; i <= i64(n); i++)

i64 INF = 100100100100100L;

i64 direct[8][2] = {
  {1, 0}, {0, -1}, {-1, 0}, {0, 1},
  {1, -1}, {-1, -1}, {-1, 1}, {1, 1}
};


template<class T, class S>
ostream &operator<<(ostream &os, pair<T, S> p);


template<class T>
ostream &operator<<(ostream &os, const vector<T> &v)
{
  for(i64 i = 0; i < i64(v.size()); i++) {
    if (i > 0) os << ' ';
    os << v[i];
  }

  return os;
}

template<class T>
ostream &operator<<(ostream &os, const set<T> &st)
{
  bool first = true;
  for(const T &it: st)
  if (first) {
      first = false;
      os << it;
  } else{
    os << ' ' << it;
  }

  return os;
}

template<class T, class S>
ostream &operator<<(ostream &os, map<T, S> &mp)
{
  bool first = true;
  for(const auto &[f, l]: mp)
  if (first) {
      first = false;
      os << '{' << f << "-> " << l << '}';
  } else{
    os << ", {" << f << "-> " << l << '}';
  }

  return os;
}


template<class T, class S>
ostream &operator<<(ostream &os, pair<T, S> p)
{
  os << '{' << p.first << ", " << p.second << '}';

  return os;
}



int main()
{
  i64 N, M, K;
  cin >> N >> M >> K;

  vector<i64> A(K);
  for(i64 &it :A) {
    cin >> it;
    it--;
  }

  VV<i64> T(N, vector<i64>(N));
  for(i64 i = 0; i < N; i++)
    for(i64 j = 0; j < N; j++)
      cin >> T[i][j];

  VV<i64> dp(N, vector<i64>(1 << N, INF));
  deque<pair<i64, i64>> deq;
  for(i64 i = 0; i < N; i++) {
    dp[i][1 << i] = 0;
    deq.push_back({i, 1 << i});
  }

  while (!deq.empty()) {

    auto [pos, bits] = deq.front();
    deq.pop_front();

    for(i64 i = 0; i < N; i++) {
      if ((bits & (1 << i)) > 0)
	continue;

      if (dp[i][(bits | (1 << i))] > dp[pos][bits] + T[pos][i]) {
	dp[i][(bits | (1 << i))] = dp[pos][bits] + T[pos][i];
	deq.push_back({i, (bits | (1 << i))});
      }	
    }
  }

  i64 ans = INF;
  for(i64 a: A) {
    for(i64 i = 1; i < (1 << N); i++)
      if (__builtin_popcount(i) >= M)
	ans = min(ans, dp[a][i]);
  }

  /*
  for(auto &v: dp) {
    for(i64 it: v)
      cout << (it < INF ? it : -1) << ' ';
    cout << endl;
  }
  */
  cout <<ans << endl;
  
  return 0;
}
0