結果
| 問題 |
No.1690 Power Grid
|
| コンテスト | |
| ユーザー |
batsumaru
|
| 提出日時 | 2021-09-24 22:30:54 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 139 ms / 3,000 ms |
| コード長 | 1,828 bytes |
| コンパイル時間 | 1,610 ms |
| コンパイル使用メモリ | 174,512 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-07-05 10:57:29 |
| 合計ジャッジ時間 | 4,683 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())
const ll INF = 1e18;
vector<vector<ll>> nxt;
// a->bの最短経路復元
// for (ll cur = a; cur != b; cur = nxt[cur][b]) {
//
// }
void warshall_floyd(vector<vector<ll>>& d) {
ll n = SZ(d);
nxt = vector<vector<ll>>(n, vector<ll>(n));
REP(i, n) REP(j, n) { nxt[i][j] = j; }
REP(i, n) REP(j, n) REP(k, n) if (d[j][k] > d[j][i] + d[i][k]) {
d[j][k] = d[j][i] + d[i][k];
nxt[j][k] = nxt[j][i];
}
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
ll n, m, k;
cin >> n >> m >> k;
vector<ll> a(n);
REP(i, n) { cin >> a[i]; }
vector<vector<ll>> d(n, vector<ll>(n, INF));
d[0][0] = 0;
REP(i, m) {
ll x, y, z;
cin >> x >> y >> z;
x--, y--;
d[x][y] = z;
d[y][x] = z;
}
warshall_floyd(d);
ll N = 1LL << n;
vector<ll> dp(N, INF);
dp[0] = 0;
REP(i, N) {
REP(j, n) {
if (i & (1LL << j)) {
continue;
}
ll ni = i | (1LL << j);
if (i == 0) {
chmin(dp[ni], dp[i] + a[j]);
} else {
ll mini = INF;
REP(k, n) {
if (i & (1LL << k)) {
chmin(mini, d[k][j]);
}
}
chmin(dp[ni], dp[i] + a[j] + mini);
}
}
}
ll ans = INF;
REP(i, N) {
ll p = 0;
REP(j, n) {
if (i & (1LL << j)) {
p++;
}
}
if (p == k) {
ans = min(ans, dp[i]);
}
}
cout << ans << endl;
}
batsumaru