結果

問題 No.1690 Power Grid
ユーザー monkukui2monkukui2
提出日時 2021-09-24 22:07:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 138 ms / 3,000 ms
コード長 2,758 bytes
コンパイル時間 957 ms
コンパイル使用メモリ 102,712 KB
実行使用メモリ 5,296 KB
最終ジャッジ日時 2023-09-18 21:25:45
合計ジャッジ時間 4,233 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 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 133 ms
5,296 KB
testcase_07 AC 133 ms
5,252 KB
testcase_08 AC 133 ms
5,224 KB
testcase_09 AC 132 ms
5,156 KB
testcase_10 AC 133 ms
5,164 KB
testcase_11 AC 132 ms
5,152 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 15 ms
4,376 KB
testcase_15 AC 135 ms
5,208 KB
testcase_16 AC 135 ms
5,216 KB
testcase_17 AC 63 ms
4,376 KB
testcase_18 AC 30 ms
4,380 KB
testcase_19 AC 135 ms
5,212 KB
testcase_20 AC 134 ms
5,252 KB
testcase_21 AC 135 ms
5,156 KB
testcase_22 AC 134 ms
5,144 KB
testcase_23 AC 135 ms
5,156 KB
testcase_24 AC 134 ms
5,208 KB
testcase_25 AC 136 ms
5,160 KB
testcase_26 AC 138 ms
5,116 KB
testcase_27 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "atcoder/modint.hpp"
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>
#include <cstddef>
#include <type_traits>
#include <vector>

using namespace std;
const long long int INF = numeric_limits<long long int>::max() / 4;
const int inf = numeric_limits<int>::max() / 4;
const long long int MOD1000000007 = 1000000007;
const long long int MOD998244353 = 998244353;
const double MATH_PI = 3.1415926535897932;

template<typename T1, typename T2>
inline void chmin(T1 &a, const T2 &b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, const T2 &b) { if (a < b) a = b; }

#define lint long long int
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define VI vector<int>
#define VLL vector<long long>
#define VC vector<char>
#define VB vector<bool>
#define PI pair<int, int>
#define PLL pair<long long, long long>
#define VPI vector<pair<int, int>>
#define VPLL vector<pair<long long, long long>>
#define VVI vector<vector<int>>
#define VVPI vecor<vector<pair<int, int>>>
#define VVPILL vector<vector<pair<int, long long>>>

#define SUM(v) accumulate(ALL(v), 0LL)
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))

int main() {

    int n, m, k; cin >> n >> m >> k;
    vector<lint> a(n);
    rep (i, n) {
        cin >> a[i];
    }

    vector<vector<lint>> g(n, vector<lint>(n, INF));
    rep (i, n) {
        g[i][i] = 0;
    }
    rep (i, m) {
        lint u, b, c; cin >> u >> b >> c;
        u--;
        b--;
        g[u][b] = g[b][u] = c;
    }
    rep (kk, n) rep (i, n) rep(j, n) {
        chmin(g[i][j], g[i][kk] + g[kk][j]);
    }

    vector<lint> dp(1 << n, INF);
    rep (i, n) {
        dp[1 << i] = a[i];
    }

    for (int bit = 0; bit < (1 << n); bit++) {
        if (dp[bit] == INF) continue;
        for (int u = 0; u < n; u++) {
            if (bit & (1 << u)) continue;
            lint nbit = bit | (1 << u);
            lint cost = INF;
            for (int v = 0; v < n; v++) {
                if (bit & (1 << v)) {
                    chmin(cost, g[u][v] + a[u]);
                }
            }
            chmin(dp[nbit], dp[bit] + cost);
        }
    }
    lint ans = INF;
    for (int bit = 0; bit < (1 << n); bit++) {
        if (__builtin_popcount(bit) == k) {
            chmin(ans, dp[bit]);
        }
    }
    cout << ans << endl;
    return 0;
}
0