結果
| 問題 |
No.1690 Power Grid
|
| コンテスト | |
| ユーザー |
siman
|
| 提出日時 | 2022-04-27 10:25:04 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 161 ms / 3,000 ms |
| コード長 | 3,113 bytes |
| コンパイル時間 | 5,255 ms |
| コンパイル使用メモリ | 144,756 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-28 07:43:21 |
| 合計ジャッジ時間 | 5,302 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
int N, M, K;
int X[20];
int Y[20];
ll Z[20];
ll dist[20][20];
class UnionFind {
public:
vector<int> _parent;
vector<int> _rank;
vector<int> _size;
UnionFind(int n) {
for (int i = 0; i < n; ++i) {
_parent.push_back(i);
_rank.push_back(0);
_size.push_back(1);
}
}
int find(int x) {
if (_parent[x] == x) {
return x;
} else {
return _parent[x] = find(_parent[x]);
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (_rank[x] < _rank[y]) {
_parent[x] = y;
_size[y] += _size[x];
} else {
_parent[y] = x;
_size[x] += _size[y];
if (_rank[x] == _rank[y]) ++_rank[x];
}
}
bool same(int x, int y) {
return find(x) == find(y);
}
int size(int x) {
return _size[find(x)];
}
};
struct Edge {
int to;
ll cost;
int from;
Edge(int to, ll cost, int from = -1) {
this->to = to;
this->cost = cost;
this->from = from;
}
bool operator>(const Edge &n) const {
return cost > n.cost;
}
};
vector<Edge> E[20];
struct Node {
int v;
ll cost;
Node(int v = -1, ll cost = -1) {
this->v = v;
this->cost = cost;
}
bool operator>(const Node &n) const {
return cost > n.cost;
}
};
void setup_dist() {
for (int v = 0; v < N; ++v) {
priority_queue <Node, vector<Node>, greater<Node>> pque;
pque.push(Node(v, 0LL));
vector<bool> visited(N, false);
while (not pque.empty()) {
Node node = pque.top();
pque.pop();
if (visited[node.v]) continue;
visited[node.v] = true;
dist[v][node.v] = node.cost;
for (Edge &e : E[node.v]) {
ll ncost = node.cost + e.cost;
pque.push(Node(e.to, ncost));
}
}
}
}
int main() {
cin >> N >> M >> K;
int x, y;
ll z;
ll A[N];
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
for (int i = 0; i < M; ++i) {
cin >> x >> y >> z;
--x;
--y;
E[y].push_back(Edge(x, z));
E[x].push_back(Edge(y, z));
}
setup_dist();
ll ans = LLONG_MAX;
for (int mask = 0; mask < (1 << N); ++mask) {
if (__builtin_popcount(mask) != K) continue;
vector<int> V;
for (int i = 0; i < N; ++i) {
if (mask >> i & 1) {
V.push_back(i);
}
}
priority_queue <Edge, vector<Edge>, greater<Edge>> pque;
ll total_cost = 0;
for (int i = 0; i < K; ++i) {
int u = V[i];
total_cost += A[u];
for (int j = i + 1; j < K; ++j) {
int v = V[j];
pque.push(Edge(v, dist[u][v], u));
}
}
UnionFind uf(N);
while (not pque.empty()) {
Edge e = pque.top();
pque.pop();
if (uf.same(e.from, e.to)) continue;
uf.unite(e.from, e.to);
total_cost += e.cost;
}
ans = min(ans, total_cost);
}
cout << ans << endl;
return 0;
}
siman