結果
問題 |
No.1690 Power Grid
|
ユーザー |
![]() |
提出日時 | 2021-09-25 04:37:36 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 155 ms / 3,000 ms |
コード長 | 1,183 bytes |
コンパイル時間 | 2,566 ms |
コンパイル使用メモリ | 195,428 KB |
最終ジャッジ日時 | 2025-01-24 18:03:57 |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 25 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define ALL(v) (v).begin(),(v).end() using ll=long long int; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12; template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;} template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} ll dp[1<<18]; int main(){ int n,m,k; cin>>n>>m>>k; vector<ll> a(n); rep(i,0,n)cin>>a[i]; ll d[20][20]; rep(i,0,n)rep(j,0,n)d[i][j]=INF; rep(i,0,n)d[i][i]=0; rep(_,0,m){ int u,v,w; cin>>u>>v>>w; u--; v--; d[u][v]=d[v][u]=w; } rep(k,0,n)rep(i,0,n)rep(j,0,n)chmin(d[i][j],d[i][k]+d[k][j]); rep(mask,0,1<<n)dp[mask]=INF; dp[0]=0; rep(mask,0,1<<n){ rep(to,0,n)if(!(mask>>to&1)){ ll add=INF; rep(from,0,n)if(mask>>from&1){ chmin(add,d[from][to]); } if(mask==0)add=0; chmin(dp[mask|(1<<to)],dp[mask]+add+a[to]); } } ll res=INF; rep(mask,0,1<<n)if(__builtin_popcountll(mask)==k)chmin(res,dp[mask]); cout<<res<<'\n'; return 0; }