結果

問題 No.1690 Power Grid
ユーザー 👑 jupirojupiro
提出日時 2021-09-24 21:26:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 153 ms / 3,000 ms
コード長 1,916 bytes
コンパイル時間 2,030 ms
コンパイル使用メモリ 205,816 KB
実行使用メモリ 5,388 KB
最終ジャッジ日時 2023-09-18 20:36:52
合計ジャッジ時間 5,597 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 147 ms
5,080 KB
testcase_07 AC 147 ms
5,072 KB
testcase_08 AC 148 ms
5,264 KB
testcase_09 AC 148 ms
5,104 KB
testcase_10 AC 147 ms
5,268 KB
testcase_11 AC 148 ms
5,196 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 16 ms
4,380 KB
testcase_15 AC 150 ms
5,388 KB
testcase_16 AC 151 ms
5,116 KB
testcase_17 AC 70 ms
4,380 KB
testcase_18 AC 33 ms
4,380 KB
testcase_19 AC 149 ms
5,084 KB
testcase_20 AC 148 ms
5,204 KB
testcase_21 AC 151 ms
5,172 KB
testcase_22 AC 149 ms
5,072 KB
testcase_23 AC 150 ms
5,256 KB
testcase_24 AC 151 ms
5,196 KB
testcase_25 AC 152 ms
5,100 KB
testcase_26 AC 153 ms
5,068 KB
testcase_27 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
using std::cin;
using std::cout;
using std::endl;
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template <class T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return 1;
  }
  return 0;
}

const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

void solve([[maybe_unused]] int CASE)
{
  int n, m, K;
  cin >> n >> m >> K;
  std::vector<ll> a(n);
  for (int i = 0; i < n; ++i)
  {
    cin >> a[i];
  }
  std::vector<std::vector<ll>> d(n, std::vector<ll>(n, INF));
  for (int i = 0; i < m; ++i)
  {
    ll x, y, z;
    cin >> x >> y >> z;
    x -= 1, y -= 1;
    chmin(d[x][y], z);
    chmin(d[y][x], z);
  }
  for (int k = 0; k < n; ++k)
  {
    for (int i = 0; i < n; ++i)
    {
      for (int j = 0; j < n; ++j)
      {
        chmin(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  std::vector<ll> dp(1 << n, INF);
  dp[0] = 0;
  for (int S = 0; S < (1 << n); ++S)
  {
    for (int i = 0; i < n; ++i)
    {
      if (S >> i & 1)
        continue;
      int nS = S | (1 << i);
      if (S == 0)
      {
        chmin(dp[nS], dp[S] + a[i]);
      }
      else
      {
        ll min = INF;
        for (int j = 0; j < n; ++j)
        {
          if (S >> j & 1)
          {
            chmin(min, d[j][i]);
          }
        }
        chmin(dp[nS], dp[S] + a[i] + min);
      }
    }
  }
  ll res = INF;
  for (int S = 0; S < (1 << n); ++S)
  {
    int cnt = 0;
    for (int i = 0; i < n; ++i)
    {
      if (S >> i & 1)
        cnt += 1;
    }
    if (cnt == K)
      chmin(res, dp[S]);
  }
  cout << res << "\n";
}
int main()
{
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int kkt = 1;
  // cin >> kkt;
  for (int jupi = 1; jupi <= kkt; jupi++)
    solve(jupi);
  return 0;
}
0