結果

問題 No.1690 Power Grid
ユーザー sten_sansten_san
提出日時 2021-09-24 22:39:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 253 ms / 3,000 ms
コード長 2,454 bytes
コンパイル時間 2,241 ms
コンパイル使用メモリ 207,028 KB
実行使用メモリ 42,172 KB
最終ジャッジ日時 2023-09-18 21:49:49
合計ジャッジ時間 7,397 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 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,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 251 ms
42,056 KB
testcase_07 AC 251 ms
42,060 KB
testcase_08 AC 251 ms
42,064 KB
testcase_09 AC 251 ms
42,068 KB
testcase_10 AC 250 ms
42,156 KB
testcase_11 AC 252 ms
42,116 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 27 ms
7,264 KB
testcase_15 AC 250 ms
42,172 KB
testcase_16 AC 251 ms
42,064 KB
testcase_17 AC 118 ms
21,472 KB
testcase_18 AC 56 ms
11,688 KB
testcase_19 AC 253 ms
41,984 KB
testcase_20 AC 253 ms
42,068 KB
testcase_21 AC 252 ms
42,044 KB
testcase_22 AC 253 ms
42,044 KB
testcase_23 AC 252 ms
42,120 KB
testcase_24 AC 251 ms
42,104 KB
testcase_25 AC 252 ms
42,060 KB
testcase_26 AC 251 ms
41,992 KB
testcase_27 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct iofast_t {
    iofast_t() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} iofast;

struct uns_t {} uns;
template <typename Element, typename Head, typename ...Args>
auto vec(Element init, Head arg, Args ...args) {
    if constexpr (sizeof...(Args) == 0) return std::vector(arg, init);
    else return std::vector(arg, vec(init, args...));
}
template <typename Element, typename Head, typename ...Args>
auto vec(uns_t, Head arg, Args ...args) {
    return vec(Element(), arg, args...);
}

template <typename T, typename Compare = less<T>>
T &chmin(T &l, T r, Compare &&f = less<T>()) { return l = min(l, r, f); }
template <typename T, typename Compare = less<T>>
T &chmax(T &l, T r, Compare &&f = less<T>()) { return l = max(l, r, f); }

int main() {
    constexpr auto inf = INT64_MAX / 2;

    int n, m, k; cin >> n >> m >> k;
    auto a = vec<int>(uns, n);
    for (auto &e : a) cin >> e;

    auto dist = vec<int64_t>(inf, n, n);
    for (int i = 0; i < n; ++i) {
        dist[i][i] = 0;
    }

    for (int i = 0; i < m; ++i) {
        int x, y, z; cin >> x >> y >> z; --x; --y;
        chmin<int64_t>(dist[x][y], z);
        chmin<int64_t>(dist[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(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }

    auto c = vec<int64_t>(inf, n, 1 << n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < (1 << n); ++j) {
            if (!(j & (1 << i))) {
                continue;
            }

            if (bitset<32>(j).count() == 1) {
                c[i][j] = a[i];
                continue;
            }

            for (int k = 0; k < n; ++k) {
                if (!(j & (1 << k)) || i == k) {
                    continue;
                }
                chmin(c[i][j], dist[i][k] + a[i]);
            }
        }
    }

    auto dp = vec<int64_t>(inf, 1 << n);

    dp[0] = 0;
    for (int i = 1; i < (1 << n); ++i) {
        for (int j = 0; j < n; ++j) {
            if (!(i & (1 << j))) {
                continue;
            }
            chmin(dp[i], dp[i ^ (1 << j)] + c[j][i]);
        }
    }

    int64_t ans = inf;
    for (int i = 0; i < (1 << n); ++i) {
        if (bitset<32>(i).count() == k) {
            chmin(ans, dp[i]);
        }
    }

    cout << ans << endl;
}

0