結果

問題 No.1690 Power Grid
ユーザー tabae326tabae326
提出日時 2021-09-24 22:44:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 244 ms / 3,000 ms
コード長 3,425 bytes
コンパイル時間 2,356 ms
コンパイル使用メモリ 212,088 KB
実行使用メモリ 44,324 KB
最終ジャッジ日時 2023-09-18 21:53:07
合計ジャッジ時間 6,250 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 121 ms
44,180 KB
testcase_07 AC 119 ms
44,176 KB
testcase_08 AC 120 ms
44,292 KB
testcase_09 AC 119 ms
44,232 KB
testcase_10 AC 31 ms
44,184 KB
testcase_11 AC 242 ms
44,288 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 17 ms
7,600 KB
testcase_15 AC 240 ms
44,316 KB
testcase_16 AC 120 ms
44,292 KB
testcase_17 AC 63 ms
22,632 KB
testcase_18 AC 25 ms
12,296 KB
testcase_19 AC 120 ms
43,916 KB
testcase_20 AC 121 ms
44,184 KB
testcase_21 AC 30 ms
44,192 KB
testcase_22 AC 226 ms
43,968 KB
testcase_23 AC 232 ms
44,308 KB
testcase_24 AC 242 ms
43,828 KB
testcase_25 AC 244 ms
43,892 KB
testcase_26 AC 243 ms
44,324 KB
testcase_27 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
// Ref: https://qiita.com/ysuzuki19/items/d89057d65284ba1a16ac
#define dump(var)  do{std::cerr << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cerr << e << "\n";}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << "\n";}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cerr << "\n"; for(const auto& v : vv){ view(v); } }
template<typename T> void dump_cout(const T& v) { for(long long i = 0; i < v.size(); i++) std::cout << v[i] << (i == v.size()-1 ? "\n" : " "); }

namespace wf {
    // There is no edge with negative cost
    template<class T>
    void warshallFloyed(std::vector<std::vector<T>> &d) {
        int n = d.size();
        for(int i = 0; i < n; i++) d[i][i] = 0;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                for(int k = 0; k < n; k++) {
                    d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
                }
            }
        }
    }
    // There are edges with negative cost.
    // If graph has negative cycle, return false
    template<class T>
    bool warshallFloyedNeg(std::vector<std::vector<T>> &d, T inf) {
        bool res = true;
        int n = d.size();
        auto chmin = [&](T &a, T b) {
            if(a == inf) a = b;
            else if(b == inf) ;
            else a = std::min(a, b);
        };
        auto addinf = [&](T a, T b) -> T {
            if(a == inf || b == inf) return inf;
            else return a + b;
        };
        for(int i = 0; i < n; i++) chmin(d[i][i], 0);
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                for(int k = 0; k < n; k++) {
                    chmin(d[j][k], addinf(d[j][i], d[i][k]));
                }
            }
        }
        for(int i = 0; i < n; i++) if(d[i][i] < 0) res = false;
        return res;
    }
};

void solve() {
    ll n, m, k;
    cin >> n >> m >> k;
    vector<ll> a(n);
    rep(i, 0, n) cin >> a[i];
    const ll inf = 1e18;
    vector<vector<pair<ll,ll>>> G(n);
    vector dist(n, vector<ll>(n, inf));
    rep(i, 0, m) {
        ll x, y, z;
        cin >> x >> y >> z;
        x--; y--;
        G[x].push_back({y, z});
        G[y].push_back({x, z});
        dist[x][y] = z;
        dist[y][x] = z;
    }
    wf::warshallFloyed(dist);
    vector dp(n+1, vector<ll>(1<<n, -1));
    auto tsp = [&](auto tsp, ll cur, ll mask) -> ll {
        if(dp[cur][mask] != -1) return dp[cur][mask];
        if(cur == k) return 0;
        ll res = inf;
        rep(i, 0, n) {
            if(mask & (1<<i)) continue;
            if(cur != 0) {
                ll mn = inf;
                rep(j, 0, n) {
                    if((mask & (1<<j)) && i != j) {
                        mn = min(mn, dist[i][j]);
                    }
                }
                if(mn == inf) continue;
                res = min(res, tsp(tsp, cur+1, mask | (1<<i)) + mn + a[i]);
            } else {
                res = min(res, tsp(tsp, cur+1, mask | (1<<i)) + a[i]);
            }
        }
        return dp[cur][mask] = res;
    };
    cout << tsp(tsp, 0, 0) << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
0