結果
問題 |
No.1690 Power Grid
|
ユーザー |
![]() |
提出日時 | 2021-09-24 22:50:25 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 127 ms / 3,000 ms |
コード長 | 1,860 bytes |
コンパイル時間 | 1,271 ms |
コンパイル使用メモリ | 121,848 KB |
最終ジャッジ日時 | 2025-01-24 17:39:06 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 25 |
ソースコード
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<bitset> #include<set> #include<map> #include<stack> #include<queue> #include<deque> #include<list> #include<iomanip> #include<cmath> #include<cstring> #include<functional> #include<cstdio> #include<cstdlib> #include<numeric> #include<ctime> //#include<atcoder/all> using namespace std; //using namespace atcoder; #define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repr(i, 0, n) #define INF 2e9 //#define MOD 1000000007 #define MOD 998244353 #define LINF (long long)4e18 #define jck 3.141592 #define PI acos(-1.0) const double EPS = 1e-18; using ll = long long; using Pi = pair<int,int>; using Pl = pair<ll,ll>; //using mint = modint998244353; int dh[] = {-1,1,0,0}; int dw[] = {0,0,1,-1}; void WarshallFloyd(vector<vector<ll>> &dist){ int N = dist.size(); rep(k,N)rep(i,N){ if(dist[i][k] == LINF) continue; rep(j,N){ if(dist[k][j] == LINF) continue; dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j]); } } } ll dp[1<<18]; int main(){ int n,m,k; cin >> n >> m >> k; vector<ll> a(n); rep(i,n) cin >> a[i]; vector dist(n,vector<ll>(n,LINF)); rep(i,m){ int x,y,z; cin >> x >> y >> z; x--; y--; dist[x][y] = dist[y][x] = z; } rep(i,n) dist[i][i] = 0; WarshallFloyd(dist); rep(i,1<<18) dp[i] = LINF; dp[0]= 0; rep(j,1<<n)rep(i,n){ if(j>>i&1) continue; ll cost = LINF; rep(l,n){ if(j>>l&1) cost = min(cost,dist[l][i]); } if(cost == LINF) cost = 0; cost += a[i]; dp[j|(1<<i)] = min(dp[j|(1<<i)],dp[j]+cost); } ll ans = LINF; rep(i,1<<n){ int cnt = __builtin_popcount(i); if(cnt == k){ ans = min(ans,dp[i]); } } cout << ans << endl; }