結果
| 問題 |
No.2200 Weird Shortest Path
|
| ユーザー |
siman
|
| 提出日時 | 2023-01-29 18:00:38 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,791 bytes |
| コンパイル時間 | 968 ms |
| コンパイル使用メモリ | 143,516 KB |
| 実行使用メモリ | 7,380 KB |
| 最終ジャッジ日時 | 2024-06-29 14:05:30 |
| 合計ジャッジ時間 | 7,448 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 16 WA * 28 |
ソースコード
#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);
uf.unite(e.from, e.to);
ans += size * e.cost;
}
cout << ans << endl;
return 0;
}
siman