結果
| 問題 |
No.1690 Power Grid
|
| コンテスト | |
| ユーザー |
kabipoyo
|
| 提出日時 | 2022-01-13 17:43:39 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,866 ms / 3,000 ms |
| コード長 | 2,233 bytes |
| コンパイル時間 | 4,270 ms |
| コンパイル使用メモリ | 254,728 KB |
| 最終ジャッジ日時 | 2025-01-27 10:45:27 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef int64_t lint;
#define rep(i, n) for(int i=0; i<n; i++)
#define repx(i, l, n) for(int i=l; i<n; i++)
#define all(v) v.begin(), v.end()
#define show(x) cout << #x << ": " << x << endl;
#define list(x) cout << #x << ": " << x << " ";
#define pb push_back
using vi = vector<lint>;
using vvi = vector<vector<lint>>;
template<class T> inline void vin(vector<T>& v) { rep(i, v.size()) cin >> v.at(i); }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline void drop(T x) { cout << x << endl; exit(0); }
template<class T> void vout(vector<T> v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; }
constexpr lint LINF = LLONG_MAX/2;
lint node, start;
vvi dist(18, vi(18)); // 事前に準備しておく
vector<vector<lint>> dp((1<<18), vector<lint>(18, -1));
vi A(18);
lint rec(lint bit, lint v) {
if (dp[bit][v] != -1) return dp[bit][v];
if (bit == (1<<v)) return dp[bit][v] = start+A[v];
lint res = LINF, prev_bit = bit & ~(1<<v);
for (lint u = 0; u < node; ++u) {
if (!(prev_bit & (1<<u))) continue;
lint d = LINF;
rep(i, node) {
if ((prev_bit & (1<<i)) && i != v) chmin(d, dist[i][v]);
}
d += rec(prev_bit, u);
chmin(res, d+A[v]);
}
return dp[bit][v] = res;
}
lint bitDP(lint N, lint s) {
node = N, start = s; // 初期コスト
lint res = LINF;
rep(i, node) chmin(res, rec((1<<node)-1, i));
return res;
}
void Floyd_Warshall(lint N, lint M) {
lint x, y, z;
rep(i, N) {
rep(j, N) {
if (i == j) dist[i][j] = 0;
else dist[i][j] = LINF;
}
}
rep(i, M) {
cin >> x >> y >> z;
x--, y--;
dist[x][y] = z;
dist[y][x] = z;
}
rep(k, N) {
rep(i, N) {
rep(j, N) {
chmin(dist[i][j], dist[i][k]+dist[k][j]);
}
}
}
}
int main() {
lint N, M, K;
cin >> N >> M >> K;
rep(i, N) cin >> A[i];
Floyd_Warshall(N, M);
lint a=LINF, b=0, c=0, x, y, z;
bitDP(N, 0);
rep(i, 1<<N) {
if (__builtin_popcount(i) == K) {
rep(j, N) {
if (dp[i][j] != -1) chmin(a, dp[i][j]);
}
}
}
std::cout << a << '\n';
}
kabipoyo