結果

問題 No.1690 Power Grid
ユーザー carrot46carrot46
提出日時 2021-09-24 22:17:00
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 143 ms / 3,000 ms
コード長 1,874 bytes
コンパイル時間 2,265 ms
コンパイル使用メモリ 206,460 KB
実行使用メモリ 5,260 KB
最終ジャッジ日時 2023-09-18 21:34:46
合計ジャッジ時間 5,429 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 137 ms
5,092 KB
testcase_07 AC 138 ms
5,188 KB
testcase_08 AC 138 ms
5,004 KB
testcase_09 AC 138 ms
5,084 KB
testcase_10 AC 138 ms
5,188 KB
testcase_11 AC 137 ms
5,232 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 15 ms
4,376 KB
testcase_15 AC 139 ms
5,184 KB
testcase_16 AC 140 ms
5,208 KB
testcase_17 AC 64 ms
4,376 KB
testcase_18 AC 31 ms
4,376 KB
testcase_19 AC 139 ms
5,208 KB
testcase_20 AC 138 ms
5,108 KB
testcase_21 AC 140 ms
5,096 KB
testcase_22 AC 139 ms
5,204 KB
testcase_23 AC 140 ms
5,260 KB
testcase_24 AC 139 ms
5,184 KB
testcase_25 AC 142 ms
5,080 KB
testcase_26 AC 143 ms
5,092 KB
testcase_27 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/modint>
using namespace atcoder;
//#pragma GCC optimize ("O3")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
using tp = tuple<ll, ll, ll>;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T b){
    if(a < b) {a = b; return true;}
    else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
    if(!v.empty()){
        for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
        std::cout<<v.back();
    }
    if(endline) std::cout<<std::endl;
}

using mint = modint998244353;
using vm = vector<mint>;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>N>>M>>K;
    vec a(N);
    rep(i, N) cin>>a[i];
    mat dist(N, vec(N, INF));
    {
        rep(i, M){
            int x, y, z;
            cin>>x>>y>>z;
            --x; --y;
            dist[x][y] = dist[y][x] = z;
        }
        rep(k, N){
            rep(i, N){
                rep(j, N) chmin(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
    ll res = INF;
    vec dp(1<<N, INF);
    dp[0] = 0;
    rep(i, N) dp[1<<i] = a[i];
    reps(s, 1, 1<<N){
        if(__builtin_popcount(s) == K) chmin(res, dp[s]);
        rep(i, N){
            if(!((s>>i)&1)){
                ll d = INF;
                rep(j, N) if((s>>j)&1) chmin(d, dist[i][j]);
                chmin(dp[s | (1<<i)], dp[s] + d + a[i]);
            }
        }
    }
    cout<<res<<endl;
}
0