結果

問題 No.1690 Power Grid
ユーザー ysystem7ysystem7
提出日時 2021-10-11 18:51:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 143 ms / 3,000 ms
コード長 2,492 bytes
コンパイル時間 2,885 ms
コンパイル使用メモリ 221,856 KB
実行使用メモリ 5,552 KB
最終ジャッジ日時 2023-10-14 03:18:15
合計ジャッジ時間 7,064 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 137 ms
5,496 KB
testcase_07 AC 137 ms
5,464 KB
testcase_08 AC 137 ms
5,496 KB
testcase_09 AC 136 ms
5,436 KB
testcase_10 AC 138 ms
5,412 KB
testcase_11 AC 137 ms
5,404 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 15 ms
4,348 KB
testcase_15 AC 139 ms
5,552 KB
testcase_16 AC 139 ms
5,428 KB
testcase_17 AC 66 ms
4,388 KB
testcase_18 AC 31 ms
4,348 KB
testcase_19 AC 138 ms
5,420 KB
testcase_20 AC 137 ms
5,432 KB
testcase_21 AC 141 ms
5,456 KB
testcase_22 AC 139 ms
5,412 KB
testcase_23 AC 139 ms
5,424 KB
testcase_24 AC 137 ms
5,452 KB
testcase_25 AC 140 ms
5,408 KB
testcase_26 AC 143 ms
5,456 KB
testcase_27 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}


const ll INF=1e18;
const int mx=200005;
ll dist[22][22];
ll dp[(1<<18)+3];
struct edge{
    ll to,cost;
};

int main(){
    //オーバーフローは大丈夫ですか??
    cin.tie(0);ios::sync_with_stdio(false);
    int n,m,k;
    cin>>n>>m>>k;
    V<ll> a(n);
    cinf(n,a);
    V<V<edge>> G(n);
    rep(i,m){
        ll a,b,c;
        cin>>a>>b>>c;
        a--;b--;
        G[a].pb({b,c});
        G[b].pb({a,c});
    }
    rep(i,n){
        rep(j,n) dist[i][j]=INF;
    }
    priority_queue<P,V<P>,greater<P>> que;
    rep(i,n){
        dist[i][i]=0;
        que.push({0,i});
        while(que.size()){
            P p=que.top();
            que.pop();
            int v=p.sc;
            if(dist[i][v]<p.ft) continue;
            for(edge e:G[v]){
                int nv=e.to;
                if(chmin(dist[i][nv],dist[i][v]+e.cost)){
                    que.push({dist[i][nv],nv});
                }
            }
        }
    }
    rep(i,(1<<n)) dp[i]=INF;
    dp[0]=0;
    rep(i,(1<<n)){
        rep(j,n){
            if(i>>j&1) continue;
            ll cst=a[j];
            if(i!=0){
                ll tmp=INF;
                rep(k,n){
                    if(i>>k&1) chmin(tmp,dist[j][k]);
                }
                cst+=tmp;
            }
            chmin(dp[i|(1<<j)],dp[i]+cst);
        }
    }
    ll ans=INF;
    rep(i,(1<<n)){
        if(__builtin_popcount(i)==k) chmin(ans,dp[i]);
    }
    out(ans);
}
0