結果

問題 No.1690 Power Grid
ユーザー merom686merom686
提出日時 2021-09-24 22:49:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 326 ms / 3,000 ms
コード長 2,081 bytes
コンパイル時間 2,490 ms
コンパイル使用メモリ 209,908 KB
実行使用メモリ 42,492 KB
最終ジャッジ日時 2023-09-18 21:55:15
合計ジャッジ時間 7,394 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,452 KB
testcase_01 AC 3 ms
9,400 KB
testcase_02 AC 6 ms
21,692 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
5,496 KB
testcase_06 AC 171 ms
42,216 KB
testcase_07 AC 170 ms
42,284 KB
testcase_08 AC 170 ms
42,148 KB
testcase_09 AC 171 ms
42,280 KB
testcase_10 AC 15 ms
42,272 KB
testcase_11 AC 326 ms
42,484 KB
testcase_12 AC 4 ms
11,456 KB
testcase_13 AC 6 ms
21,904 KB
testcase_14 AC 30 ms
32,292 KB
testcase_15 AC 324 ms
42,436 KB
testcase_16 AC 172 ms
42,328 KB
testcase_17 AC 96 ms
37,196 KB
testcase_18 AC 41 ms
34,700 KB
testcase_19 AC 171 ms
42,492 KB
testcase_20 AC 170 ms
42,364 KB
testcase_21 AC 15 ms
42,236 KB
testcase_22 AC 317 ms
42,344 KB
testcase_23 AC 322 ms
42,308 KB
testcase_24 AC 323 ms
42,252 KB
testcase_25 AC 321 ms
42,368 KB
testcase_26 AC 323 ms
42,372 KB
testcase_27 AC 3 ms
9,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct Graph {
    static constexpr ll INF = 1LL << 60;
    struct Edge {
        int d, i;
    };
    Graph(int n) : e(n), d(n, vector<ll>(n, INF)), n(n) {
        for (int i = 0; i < n; i++) d[i][i] = 0;
    }
    void add_edge(int i, int j, int l) {
        e[i].push_back({ l, j });
        d[i][j] = l;//多重辺はないとする
    }
    void warshall_floyd() {
        for (int k = 0; k < n; k++)
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    }
    vector<vector<Edge>> e;
    vector<vector<ll>> d;
    int n;
};

ll dp[19][1 << 18];

int main() {
    int n, m, K;
    cin >> n >> m >> K;

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    Graph g(n);
    for (int j = 0; j < m; j++) {
        int x, y, z;
        cin >> x >> y >> z;
        x--; y--;

        g.add_edge(x, y, z);
        g.add_edge(y, x, z);
    }
    g.warshall_floyd();

    ll r = 1LL << 60;
    for (int i = 0; i < n; i++) {
        for (int k = 0; k < 1 << n; k++) {
            dp[i][k] = 1LL << 60;
        }
        dp[i][1 << i] = a[i];
        if (1 == K) r = min(r, dp[i][1 << i]);
    }
    for (int k = 0; k < 1 << n; k++) {
        int c = __builtin_popcount(k);
        if (c == 1 || c > K) continue;
        for (int i = 0; i < n; i++) {
            if (k & 1 << i) {
                int l = k ^ 1 << i;
                ll t = 1LL << 60;
                for (int j = 0; j < n; j++) {
                    if (l & 1 << j) {
                        t = min(t, dp[j][l]);
                    }
                }
                ll s = 1LL << 60;
                for (int j = 0; j < n; j++) {
                    if (l & 1 << j) {
                        s = min(s, g.d[i][j]);
                    }
                }
                dp[i][k] = t + a[i] + s;
                if (c == K) r = min(r, dp[i][k]);
            }
        }
    }
    cout << r << endl;

    return 0;
}
0