結果

問題 No.1690 Power Grid
コンテスト
ユーザー ryoku
提出日時 2026-09-26 07:12:34
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 410 ms / 3,000 ms
+ 367µs
コード長 14,141 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,793 ms
コンパイル使用メモリ 322,824 KB
実行使用メモリ 9,872 KB
最終ジャッジ日時 2026-09-26 07:12:46
合計ジャッジ時間 10,485 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#define CP_BUNDLED_SOURCE
#ifndef CP_BUNDLE_HEADER_3840A09F237A779E
#define CP_BUNDLE_HEADER_3840A09F237A779E
#ifdef TEMPLATE
#else
#define TEMPLATE
# pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <climits>
#include <cassert>
#include <functional>
#include <iterator>
#include <utility>
#include <complex>
#include <bitset>
#include <chrono>
#include <random>
#include <limits>
#include <optional>
#include <variant>
#include <any>

#include <array>
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <version>
using namespace std;
using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using i128=__int128;
using u128=unsigned __int128;
template<class T>using vc=vector<T>;
template<class T>using vvc=vc<vc<T>>;
template<class T>using vvvc=vvc<vc<T>>;
template<class T>using smpq=priority_queue<T,vector<T>,greater<T>>;
template<class T>using bipq=priority_queue<T>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)
#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)
#define drep(i,n) for(ll i=((n)-1);i>=0;i--)
#define rall(x) x.rbegin(),x.rend()
#define mp(...) make_pair(__VA_ARGS__)
#define pb push_back
#define fi first
#define se second
#define is insert
#define bg begin()
#define ed end()
#define all(x) x.begin(),x.end()
void scan(int&a) { cin >> a; }
void scan(ll&a) { cin >> a; }
void scan(string&a) { cin >> a; }
void scan(char&a) { cin >> a; }
void scan(uint&a) { cin >> a; }
void scan(ull&a) { cin >> a; }
void scan(bool&a) { cin >> a; }
void scan(ld&a){ cin>> a;}
template<class T> void scan(vector<T>&a) { for(auto&x:a) scan(x); }
void read() {}
template<class Head, class... Tail> void read(Head&head, Tail&... tail) { scan(head); read(tail...); }
#define INT(...) int __VA_ARGS__; read(__VA_ARGS__);
#define LL(...) ll __VA_ARGS__; read(__VA_ARGS__);
#define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__);
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define VC(type, name, ...) vector<type> name(__VA_ARGS__); read(name);
#define VVC(type, name, size, ...) vector<vector<type>> name(size, vector<type>(__VA_ARGS__)); read(name);
template<class T>void print(T a) { cout << a; }
template<class T> void print(vector<T>a) { for(int i=0;i<(int)a.size();i++){if(i)cout<<" ";print(a[i]);}}
void PRT() { cout <<endl; return ; }
template<class T> void PRT(T a) { print(a); cout <<endl; return; }
template<class Head, class... Tail> void PRT(Head head, Tail ... tail) { print(head); cout << " "; PRT(tail...); return; }
template<class T,class F>
bool chmin(T &x, F y){
    if(x>y){
        x=y;
        return true;
    }
    return false;
}
template<class T, class F>
bool chmax(T &x, F y){
    if(x<y){
        x=y;
        return true;
    }
    return false;
}
template<typename T>
T floor(T a,T b){
    return a/b-(a%b&&((a<0)!=(b<0)));
}
template<typename T>
T ceil(T a,T b){
    return a/b+(a%b&&((a<0)==(b<0)));
}
template<typename T>
T bmod(T x,T y){
    return x-y*floor(x,y);
}
template<typename T>
pair<T,T>divmod(T x,T y){
    T q=floor(x,y);
    return{q,x-q*y};
}
void YesNo(bool b){
    cout<<(b?"Yes":"No")<<endl;
}
void YESNO(bool b){
    cout<<(b?"YES":"NO")<<endl;
}
void AliceBob(bool b){
    cout<<(b?"Alice":"Bob")<<endl;
}
void TakahashiAoki(bool b){
    cout<<(b?"Takahashi":"Aoki")<<endl;
}
void Yes(){
    cout<<"Yes"<<endl;
}
void No(){
    cout<<"No"<<endl;
}
vc<int>stovi(const string&s,const string&S){
    vc<int>v(s.size());
    rep(i,s.size()){
        auto t=S.find(s[i]);
        assert(t!=string::npos);
        v[i]=t;
    }
    return v;
}
template<class T=ll>
T isqrt(T x){
    T F=sqrtl(x);
    while((F+1)*(F+1)<=x)F++;
    while(F*F>x)F--;
    return F;
}
template<class T>
T tri(T x){return x*(x-1)/2;}

//[l,r)
template<class T>T tri(T l,T r){return (r-l)*(l+r-1)/2;}
//n を先頭に持ってくる
template<class T>
vc<T>rot(vc<T>v,int n){
    rotate(v.begin(),v.begin()+n,v.end());
    return v;
}
template<class T>
vc<T>iota(int n){
    vc<T>v(n);rep(i,n)v[i]=i;
    return v;
}
template<class T,int minfirst>
vc<int>argsort(const vc<T>&a){
    auto idx=iota<int>(a.size());
    sort(all(idx),[&](int i,int j){
        return (minfirst?make_pair(a[i],i)<make_pair(a[j],j):make_pair(a[i],i)>make_pair(a[j],j));
    });
    return idx;
}
template<class T>
vvc<T>trans(const vvc<T>&a){
    assert(a.size()&&a[0].size());
    rep(i,a.size())assert(a[i].size()==a[0].size());
    vvc<T>b(a[0].size(),vc<T>(a.size()));
    rep(i,a.size())rep(j,a[0].size())b[j][i]=a[i][j];
    return b;
}
vc<string>trans(const vc<string>&a){
    assert(a.size()&&a[0].size());
    rep(i,a.size())assert(a[i].size()==a[0].size());
    vc<string>b(a[0].size(),string(a.size(),0));
    rep(i,a.size())rep(j,a[0].size())b[j][i]=a[i][j];
    return b;
}
template<class T>
int popcount(T n){
    return __builtin_popcountll(n);
}
template<class T,class L=ll>
L sum(const vc<T>&a){
    return accumulate(all(a),L(0));
}
template<class T>
struct subset_view{
    T s;
    struct iterator{
        T s,x;
        bool done;
        T operator*()const{return x;}
        iterator&operator++(){
            if(x==0)done=true;
            else x=(x-1)&s;
            return*this;
        }
        bool operator!=(const iterator&r)const{return done!=r.done;}
    };
    iterator begin()const{return{s,s,false};}
    iterator end()const{return{s,0,true};}
};
template<class T>
subset_view<T>subset(T s){
    return{s};
}
template<class T>
T max(vc<T>&a){
    return *max_element(all(a));
}
template<class T>
T min(vc<T>&a){
    return *min_element(all(a));
}
template<class T,class L=ll>
vc<L> presum(vc<T> &a){
    vc<L> ret(a.size()+1);
    rep(i,a.size())ret[i+1]=ret[i]+a[i];
    return ret;
}
template<class T, class F>
vc<T> &operator+=(vc<T> &a,F b){
    for (auto&v:a)v += b;
    return a;
}
template<class T, class F>
vc<T> &operator-=(vc<T>&a,F b){
    for (auto&v:a)v-=b;
    return a;
}
template<class T, class F>
vc<T> &operator*=(vc<T>&a,F b){
    for (auto&v:a)v*=b;
    return a;
}
template<class T=ll>
constexpr T pow(T a,T b){
    T res=1;
    while(b){
        if(b&1)res*=a;
        a*=a;
        b/=2;
    }
    return res;
}
constexpr ll ten(ll a){
    return pow<ll>(10,a);
}
template<typename T>constexpr T inf=numeric_limits<T>::max()/2-1;
template<class T>
int tbit(T x){
    using U=make_unsigned_t<T>;
    U y=(U)x;
    return y?(int)bit_width(y)-1:-1;
}
template<class T>
int lbit(T x){
    using U=make_unsigned_t<T>;
    U y=(U)x;
    return y?(int)countr_zero(y):-1;
}
template<class T>
int tbit(T x,int p){
    using U=make_unsigned_t<T>;
    constexpr int W=numeric_limits<U>::digits;
    U y=(U)x;
    if(p<0)return -1;
    if(p>=W-1)return tbit(y);
    return tbit(y&((U(1)<<(p+1))-1));
}
template<class T>
int lbit(T x,int p){
    using U=make_unsigned_t<T>;
    constexpr int W=numeric_limits<U>::digits;
    U y=(U)x;
    if(p<0)return lbit(y);
    if(p>=W)return -1;
    return lbit(y&(~U(0)<<p));
}
istream& operator>>(istream&is,i128&x){
    string s;is>>s;
    x=0;
    int i=0,neg=0;
    if(s[0]=='-')neg=1,i=1;
    for(;i<(int)s.size();i++)x=x*10+s[i]-'0';
    if(neg)x=-x;
    return is;
}
ostream& operator<<(ostream&os,i128 x){
    if(x==0)return os<<0;
    if(x<0)os<<"-";
    u128 y=x<0?-(u128)x:(u128)x;
    string s;
    while(y)s.pb('0'+y%10),y/=10;
    reverse(all(s));
    return os<<s;
}
#ifdef LOCAL
#include "debug/debug.hpp"
inline int dbging=1;
#else
#define dbg(...) ((void)0)
#endif

struct template_setup{
    template_setup(){
        #ifdef LOCAL
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
        
        #endif
        cin.tie(0)->sync_with_stdio(0);
        #ifdef LOCAL
        cout<<fixed<<setprecision(6);
        #else
        cout<<fixed<<setprecision(20);
        #endif
    }
};
inline template_setup template_setup;

#if defined(LOCAL)&&!defined(CP_NO_AUTO_ALL)&&!defined(CP_BUNDLED_SOURCE)
#include <all.hpp>
#endif

#endif
#endif
#ifndef CP_BUNDLE_HEADER_A415336C78CC3AB3
#define CP_BUNDLE_HEADER_A415336C78CC3AB3
struct unweighted{
    unweighted()=default;
    unweighted(int){}
    operator int()const{return 1;}
};
template<class T=unweighted>
struct edge{
    int from,to,id;
    [[no_unique_address]]T cost;
#ifdef LOCAL
    friend ostream&operator<<(ostream&os,const edge&e){
        return os<<"{from:"<<e.from<<",to:"<<e.to<<",id:"<<e.id<<",cost:"<<e.cost<<"}";
    }
#endif
};
template<bool is_directed,class T=unweighted>
struct static_graph{
    constexpr static bool directed(){return is_directed;}
    using edge=::edge<T>;
    using cost_t=T;
private:
    int n;
    mutable bool built=false,inv_built=false;
    vc<edge>edges;
    mutable vc<int>start,inv_start;
    mutable vc<edge>csr,inv_csr;
public:
    static_graph(int n):n(n),start(n+1),inv_start(n+1){assert(n>=0);}
    static_graph(int n,int m):static_graph(n){assert(m>=0);edges.reserve(m);}
    void resize(int size){
        assert(n<=size&&!built);
        assert(!inv_built);
        n=size;
        start.resize(n+1);
        inv_start.resize(n+1);
    }
    void add_edge(const edge&e){
        assert(!built&&!inv_built);
        assert(0<=e.from&&e.from<n&&0<=e.to&&e.to<n);
        edges.pb(e);
    }
    void add_edge(int a,int b,cost_t cost=1,int id=-1){
        assert(!built&&!inv_built);
        assert(0<=a&&a<n&&0<=b&&b<n);
        if(id==-1)id=edges.size();
        edges.pb({a,b,id,cost});
    }
    template<int substract=0>
    void input(int m){
        assert(m>=0);
        rep(i,m){
            INT(a,b);
            a-=substract;b-=substract;
            add_edge(a,b);
        }
        build();
    }
    void build()const{
        if(built)return;
        built=true;
        start.assign(n+1,0);
        for(auto&e:edges){
            ++start[e.from];
            if constexpr(!is_directed)++start[e.to];
        }
        rep(i,n)start[i+1]+=start[i];
        csr.resize(start[n]);
        for(auto it=edges.rbegin();it!=edges.rend();++it){
            auto&e=*it;
            csr[--start[e.from]]=e;
            if constexpr(!is_directed)csr[--start[e.to]]={e.to,e.from,e.id,e.cost};
        }
    }
    void buildinv()const{
        if(inv_built)return;
        inv_start.assign(n+1,0);
        for(auto&e:edges){
            ++inv_start[e.to];
            if constexpr(!is_directed)++inv_start[e.from];
        }
        rep(i,n)inv_start[i+1]+=inv_start[i];
        inv_csr.resize(inv_start[n]);
        for(auto it=edges.rbegin();it!=edges.rend();++it){
            auto&e=*it;
            inv_csr[--inv_start[e.to]]={e.to,e.from,e.id,e.cost};
            if constexpr(!is_directed)inv_csr[--inv_start[e.from]]={e.from,e.to,e.id,e.cost};
        }
        inv_built=true;
    }
    auto operator[](int u){
        build();
        assert(0<=u&&u<n);
        return span<edge>(csr.data()+start[u],start[u+1]-start[u]);
    }

    auto operator[](int u)const{
        build();
        assert(0<=u&&u<n);
        return span<const edge>(csr.data()+start[u],start[u+1]-start[u]);
    }

    auto inv(int u){
        buildinv();
        assert(0<=u&&u<n);
        return span<edge>(inv_csr.data()+inv_start[u],inv_start[u+1]-inv_start[u]);
    }

    auto inv(int u)const{
        buildinv();
        assert(0<=u&&u<n);
        return span<const edge>(inv_csr.data()+inv_start[u],inv_start[u+1]-inv_start[u]);
    }

    const vc<edge>&all_edges()const{return edges;}
    int edge_size()const{return edges.size();}

    edge get_edge(int id)const{
        assert(0<=id&&id<edge_size());
        return edges[id];
    }
    int out_deg(int u)const{
        build();
        assert(0<=u&&u<n);
        return start[u+1]-start[u];
    }
    int in_deg(int u)const{
        buildinv();
        assert(0<=u&&u<n);
        return inv_start[u+1]-inv_start[u];
    }
    int deg(int u)const{return out_deg(u);}
    int size()const{return n;}
    template<class F>
    vvc<F>adj()const{
        vvc<F>res(n,vc<F>(n));
        for(auto&e:edges){
            res[e.from][e.to]=e.cost;
            if constexpr(!is_directed)res[e.to][e.from]=e.cost;
        }
        return res;
    }
    void clear(){
        built=false;
        inv_built=false;
        edges.clear();
        csr.clear();
        inv_csr.clear();
        start.assign(n+1,0);
        inv_start.assign(n+1,0);
    }
    template<class F>
    void sort(int i,F f){
        build();
        assert(0<=i&&i<n);
        std::sort(csr.begin()+start[i],csr.begin()+start[i+1],f);
    }
};
#endif
template<class T,class Graph,class F>
vc<T>slow_dijkstra(const Graph&g,vc<F>starts){
    int n=g.size();
    vc<T>md(n,inf<T>);
    for(auto&x:starts)md[x]=0;
    vc<int>done(n);
    rep(i,n){
        pair<T,int>target{inf<T>,-1};
        rep(j,n){
            if(!done[j]){
                chmin(target,pair<T,int>{md[j],j});
            }
        }
        if(target.se==-1)continue;
        done[target.se]=1;
        for(auto&e:g[target.se]){
            chmin(md[e.to],md[target.se]+e.cost);
        }
    }
    return md;
}
void solve(){
    LL(n,m,k);
    VC(ll,a,n);
    static_graph<0,ll>g(n);
    rep(i,m){
        LL(x,y,z);--x,--y;g.add_edge(x,y,z);
    }
    vc<ll>dp(1<<n,inf<ll>);dp[0]=0;
    rep(i,1<<n){
        if(i==0){
            rep(j,n)dp[1<<j]=a[j];
            continue;
        }
        vc<ll>start;rep(j,n)if(i>>j&1)start.pb(j);
        auto res=slow_dijkstra<ll>(g,start);
        rep(add,n){
            if(i>>add&1)continue;
            chmin(dp[i+(1<<add)],dp[i]+res[add]+a[add]);
        }
    }
    ll ans=inf<ll>;rep(i,1<<n)if(popcount(i)==k)chmin(ans,dp[i]);
    PRT(ans);;
}
signed main(){
    int t=1;
    // cin >> t;
    while(t--)solve();
}
0