結果

問題 No.1690 Power Grid
ユーザー batsumarubatsumaru
提出日時 2021-09-24 22:30:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 139 ms / 3,000 ms
コード長 1,828 bytes
コンパイル時間 1,598 ms
コンパイル使用メモリ 172,816 KB
実行使用メモリ 5,408 KB
最終ジャッジ日時 2023-09-18 21:45:58
合計ジャッジ時間 4,937 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 134 ms
5,212 KB
testcase_07 AC 134 ms
5,264 KB
testcase_08 AC 135 ms
5,100 KB
testcase_09 AC 134 ms
5,136 KB
testcase_10 AC 134 ms
5,180 KB
testcase_11 AC 134 ms
5,180 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 15 ms
4,380 KB
testcase_15 AC 136 ms
5,264 KB
testcase_16 AC 137 ms
5,100 KB
testcase_17 AC 64 ms
4,376 KB
testcase_18 AC 30 ms
4,376 KB
testcase_19 AC 136 ms
4,380 KB
testcase_20 AC 136 ms
5,408 KB
testcase_21 AC 137 ms
5,136 KB
testcase_22 AC 135 ms
5,104 KB
testcase_23 AC 136 ms
5,200 KB
testcase_24 AC 135 ms
5,084 KB
testcase_25 AC 138 ms
5,148 KB
testcase_26 AC 139 ms
5,140 KB
testcase_27 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

const ll INF = 1e18;
vector<vector<ll>> nxt;
// a->bの最短経路復元
// for (ll cur = a; cur != b; cur = nxt[cur][b]) {
//
// }
void warshall_floyd(vector<vector<ll>>& d) {
  ll n = SZ(d);
  nxt = vector<vector<ll>>(n, vector<ll>(n));
  REP(i, n) REP(j, n) { nxt[i][j] = j; }

  REP(i, n) REP(j, n) REP(k, n) if (d[j][k] > d[j][i] + d[i][k]) {
    d[j][k] = d[j][i] + d[i][k];
    nxt[j][k] = nxt[j][i];
  }
}

template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  REP(i, n) { cin >> a[i]; }
  vector<vector<ll>> d(n, vector<ll>(n, INF));
  d[0][0] = 0;
  REP(i, m) {
    ll x, y, z;
    cin >> x >> y >> z;
    x--, y--;
    d[x][y] = z;
    d[y][x] = z;
  }
  warshall_floyd(d);

  ll N = 1LL << n;
  vector<ll> dp(N, INF);
  dp[0] = 0;
  REP(i, N) {
    REP(j, n) {
      if (i & (1LL << j)) {
        continue;
      }
      ll ni = i | (1LL << j);
      if (i == 0) {
        chmin(dp[ni], dp[i] + a[j]);
      } else {
        ll mini = INF;
        REP(k, n) {
          if (i & (1LL << k)) {
            chmin(mini, d[k][j]);
          }
        }
        chmin(dp[ni], dp[i] + a[j] + mini);
      }
    }
  }

  ll ans = INF;
  REP(i, N) {
    ll p = 0;
    REP(j, n) {
      if (i & (1LL << j)) {
        p++;
      }
    }
    if (p == k) {
      ans = min(ans, dp[i]);
    }
  }
  cout << ans << endl;
}
0