結果

問題 No.1690 Power Grid
ユーザー ktr216ktr216
提出日時 2021-09-24 23:21:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 196 ms / 3,000 ms
コード長 2,046 bytes
コンパイル時間 2,328 ms
コンパイル使用メモリ 178,988 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 22:23:28
合計ジャッジ時間 4,932 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 152 ms
4,380 KB
testcase_07 AC 152 ms
4,380 KB
testcase_08 AC 152 ms
4,380 KB
testcase_09 AC 151 ms
4,376 KB
testcase_10 AC 17 ms
4,380 KB
testcase_11 AC 18 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 21 ms
4,376 KB
testcase_15 AC 18 ms
4,376 KB
testcase_16 AC 181 ms
4,380 KB
testcase_17 AC 98 ms
4,384 KB
testcase_18 AC 41 ms
4,380 KB
testcase_19 AC 175 ms
4,380 KB
testcase_20 AC 196 ms
4,380 KB
testcase_21 AC 18 ms
4,380 KB
testcase_22 AC 91 ms
4,380 KB
testcase_23 AC 18 ms
4,380 KB
testcase_24 AC 29 ms
4,380 KB
testcase_25 AC 20 ms
4,380 KB
testcase_26 AC 19 ms
4,384 KB
testcase_27 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = (l); i < (r); i++)
using namespace std;

typedef long long ll;

const int MAX_N = 100010;
int par[MAX_N];
int rnk[MAX_N];
int siz[MAX_N];

void init(int n) {
    rep(i,0,n) {
        par[i] = i;
        rnk[i] = 0;
        siz[i] = 1;
    }
}

int find(int x) {
    if (par[x] == x) {
        return x;
    } else {
        return par[x] = find(par[x]);
    }
}

void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    int s = siz[x] + siz[y];
    if (rnk[x] < rnk[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if (rnk[x] == rnk[y]) rnk[x]++;
    }
    siz[find(x)] = s;
}

bool same(int x, int y) {
    return find(x) == find(y);
}

int size(int x) {
    return siz[find(x)];
}

int main() {
    int N, M, K;
    cin >> N >> M >> K;
    vector<ll> A(N), X(M), Y(M), Z(M);
    vector<vector<ll>> d(N, vector<ll>(N, 1e18));
    rep(i, 0, N) {
        cin >> A[i];
        d[i][i] = 0;
    }
    rep(i, 0, M) {
        cin >> X[i] >> Y[i] >> Z[i];
        d[X[i] - 1][Y[i] - 1] = Z[i];
        d[Y[i] - 1][X[i] - 1] = Z[i];
    }
    rep(k, 0, N) rep(i, 0, N) rep(j, 0, N) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    ll ans = 1e18;
    rep(i, 0, 1 << N) {
        ll popcount = 0, Asum = 0;
        rep(j, 0, N) {
            if ((i >> j) & 1) {
                popcount++;
                Asum += A[j];
            }
        }
        if (popcount != K) continue;
        init(N);
        vector<vector<ll>> vc;
        rep(j, 0, N) {
            if (((i >> j) & 1) == 0) continue;
            rep(k, 0, j) {
                if (((i >> k) & 1) == 0) continue;
                vc.push_back({d[j][k], j, k});
            }
        }
        sort(vc.begin(), vc.end());
        ll roadcost = 0;
        rep(i, 0, vc.size()) {
            if (same(vc[i][1], vc[i][2])) continue;
            unite(vc[i][1], vc[i][2]);
            roadcost += vc[i][0];
        }
        ans = min(ans, Asum + roadcost);
    }
    cout << ans << endl;
}
0