結果

問題 No.1690 Power Grid
ユーザー merom686merom686
提出日時 2021-09-24 22:44:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,884 bytes
コンパイル時間 2,276 ms
コンパイル使用メモリ 208,580 KB
実行使用メモリ 42,480 KB
最終ジャッジ日時 2023-09-18 21:53:01
合計ジャッジ時間 5,503 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,348 KB
testcase_01 AC 3 ms
9,676 KB
testcase_02 AC 5 ms
21,772 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
5,468 KB
testcase_06 AC 100 ms
42,168 KB
testcase_07 AC 101 ms
42,172 KB
testcase_08 AC 102 ms
42,144 KB
testcase_09 AC 101 ms
42,296 KB
testcase_10 AC 14 ms
42,300 KB
testcase_11 WA -
testcase_12 AC 3 ms
11,528 KB
testcase_13 AC 6 ms
21,796 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 101 ms
42,156 KB
testcase_21 AC 14 ms
42,236 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 3 ms
9,672 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] + g.d[i][j]);
                    }
                }
                dp[i][k] = t + a[i];
                if (c == K) r = min(r, dp[i][k]);
            }
        }
    }
    cout << r << endl;

    return 0;
}
0