結果

問題 No.1690 Power Grid
ユーザー milanis48663220milanis48663220
提出日時 2021-09-24 22:25:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 149 ms / 3,000 ms
コード長 2,333 bytes
コンパイル時間 1,215 ms
コンパイル使用メモリ 123,328 KB
実行使用メモリ 5,364 KB
最終ジャッジ日時 2023-09-18 21:43:00
合計ジャッジ時間 4,743 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 144 ms
5,364 KB
testcase_07 AC 145 ms
5,216 KB
testcase_08 AC 145 ms
5,160 KB
testcase_09 AC 145 ms
5,220 KB
testcase_10 AC 146 ms
5,120 KB
testcase_11 AC 143 ms
5,216 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 15 ms
4,380 KB
testcase_15 AC 146 ms
5,204 KB
testcase_16 AC 148 ms
5,112 KB
testcase_17 AC 68 ms
4,376 KB
testcase_18 AC 33 ms
4,380 KB
testcase_19 AC 146 ms
5,100 KB
testcase_20 AC 145 ms
5,232 KB
testcase_21 AC 147 ms
5,104 KB
testcase_22 AC 146 ms
5,120 KB
testcase_23 AC 146 ms
5,104 KB
testcase_24 AC 145 ms
5,212 KB
testcase_25 AC 148 ms
5,232 KB
testcase_26 AC 149 ms
5,212 KB
testcase_27 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

const ll INF = 1e+15;

vector<vector<ll>> warshallfloyd(vector<vector<ll>> g){
    int n = g.size();
    vector<vector<ll>> dist(n, vector<ll>(n));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            dist[i][j] = g[i][j];
        }
        dist[i][i] = 0;
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            for(int k = 0; k < n; k++){
                ll new_len = dist[j][i] + dist[i][k];
                dist[j][k] = min(new_len, dist[j][k]);
            }
        }
    }
    return dist;
}

int popcount(int n){
    int ans = 0;
    for(int i = 0; i < 20; i++){
        if(n&(1<<i)) ans++;
    }
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m, k; cin >> n >> m >> k;
    vector<ll> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    vector<vector<ll>> g(n, vector<ll>(n, INF));
    for(int i = 0; i < m; i++){
        int x, y; ll z;
        cin >> x >> y >> z; x--; y--;
        g[x][y] = z;
        g[y][x] = z;
    }
    auto d = warshallfloyd(g);
    vector<ll> dp(1<<n, INF);
    dp[0] = 0;
    for(int i = 0; i < n; i++){
        dp[1<<i] = a[i];
    }
    for(int s = 1; s < (1<<n); s++){
        for(int i = 0; i < n; i++){
            if(s&(1<<i)) continue;
            ll min_d = INF;
            for(int j = 0; j < n; j++){
                if((s&(1<<j)) == 0) continue;
                chmin(min_d, d[i][j]);
            }
            chmin(dp[s+(1<<i)], dp[s]+min_d+a[i]);
        }
    }
    ll ans = INF;
    for(int s = 0; s < (1<<n); s++){
        if(popcount(s) == k) chmin(ans, dp[s]);
    }
    cout << ans << endl;
}
0