結果

問題 No.2200 Weird Shortest Path
ユーザー simansiman
提出日時 2023-01-29 18:01:59
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 1,790 bytes
コンパイル時間 1,259 ms
コンパイル使用メモリ 107,092 KB
実行使用メモリ 7,428 KB
最終ジャッジ日時 2023-09-12 00:58:43
合計ジャッジ時間 8,325 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,504 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 70 ms
4,384 KB
testcase_18 AC 124 ms
5,944 KB
testcase_19 AC 213 ms
6,260 KB
testcase_20 AC 205 ms
6,784 KB
testcase_21 AC 81 ms
5,492 KB
testcase_22 AC 111 ms
5,248 KB
testcase_23 AC 222 ms
6,548 KB
testcase_24 AC 38 ms
4,380 KB
testcase_25 AC 208 ms
6,240 KB
testcase_26 AC 217 ms
6,504 KB
testcase_27 AC 75 ms
4,976 KB
testcase_28 AC 164 ms
6,264 KB
testcase_29 AC 220 ms
7,024 KB
testcase_30 AC 222 ms
6,312 KB
testcase_31 AC 133 ms
5,416 KB
testcase_32 AC 224 ms
7,260 KB
testcase_33 AC 225 ms
7,260 KB
testcase_34 AC 225 ms
7,356 KB
testcase_35 AC 225 ms
7,324 KB
testcase_36 AC 224 ms
7,300 KB
testcase_37 AC 222 ms
7,428 KB
testcase_38 AC 225 ms
7,336 KB
testcase_39 AC 113 ms
5,996 KB
testcase_40 AC 166 ms
7,428 KB
testcase_41 AC 98 ms
5,948 KB
testcase_42 AC 152 ms
7,336 KB
testcase_43 AC 212 ms
7,356 KB
testcase_44 AC 207 ms
7,300 KB
testcase_45 AC 205 ms
7,304 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;

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;
    ll size = uf.size(e.from) * uf.size(e.to);
    uf.unite(e.from, e.to);

    ans += size * e.cost;
  }

  cout << ans << endl;

  return 0;
}

0