結果

問題 No.2200 Weird Shortest Path
ユーザー simansiman
提出日時 2023-01-29 17:56:24
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,864 bytes
コンパイル時間 4,471 ms
コンパイル使用メモリ 107,580 KB
実行使用メモリ 7,768 KB
最終ジャッジ日時 2023-09-12 00:55:27
合計ジャッジ時間 14,260 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
4,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 99 ms
5,944 KB
testcase_42 AC 151 ms
7,368 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
権限があれば一括ダウンロードができます

ソースコード

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;

const int MAX_N = 200000;
vector<int> _parent;
vector<int> _rank;
vector<int> _size;

class UnionFind {
public:
  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 from;
  int to;
  int cost;

  Edge(int from = -1, int to = -1, int cost = -1) {
    this->from = from;
    this->to = to;
    this->cost = cost;
  }

  bool operator>(const Edge &e) const {
    return cost > e.cost;
  }
};

int main() {
  int N, M;
  cin >> N >> M;

  priority_queue <Edge, vector<Edge>, greater<Edge>> pque;
  for (int i = 0; i < M; ++i) {
    int a, b, w;
    cin >> a >> b >> w;

    pque.push(Edge(a, b, w));
  }

  ll ans = 0;
  UnionFind uf(N + 1);
  while (not pque.empty()) {
    Edge e = pque.top();
    pque.pop();

    if (uf.same(e.from, e.to)) continue;
    int size = uf.size(e.from) + uf.size(e.to);
    if (uf.size(e.from) == 1 || uf.size(e.to) == 1) {
      --size;
    }
    uf.unite(e.from, e.to);

    ans += size * e.cost;
  }

  cout << ans << endl;

  return 0;
}
0