結果

問題 No.1690 Power Grid
ユーザー simansiman
提出日時 2022-04-27 10:25:04
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 163 ms / 3,000 ms
コード長 3,113 bytes
コンパイル時間 4,367 ms
コンパイル使用メモリ 106,084 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-10 16:33:01
合計ジャッジ時間 5,781 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 95 ms
4,388 KB
testcase_07 AC 95 ms
4,380 KB
testcase_08 AC 101 ms
4,388 KB
testcase_09 AC 100 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 17 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 140 ms
4,376 KB
testcase_17 AC 85 ms
4,376 KB
testcase_18 AC 34 ms
4,380 KB
testcase_19 AC 142 ms
4,376 KB
testcase_20 AC 163 ms
4,380 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 56 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 10 ms
4,376 KB
testcase_25 AC 4 ms
4,380 KB
testcase_26 AC 3 ms
4,388 KB
testcase_27 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0