在重载结构体里的“小于”关系时报了个错
我之前也写过一个重载结构体里的小于号但是这个没报错
怎么一回事呢?
#include
#include
#include
#include
using namespace std;
struct edge {
int from;
int to;
int weight;
edge(int a, int b, int c) : from(a), to(b), weight(c) {}
bool operator< ( edge n) const {
return weight < n.weight;
}
};
vector<int>fathers;
vector<int>height;
priority_queue, greater> q;
int find(int i) {
if (fathers[i] != i) {
return find(fathers[i]);
}
else {
return i;
}
}
void Union(int a, int b) {
int x = fathers[a];
int y = fathers[b];
if (height[x] > height[y]) {
fathers[y] = x;
}
else if (height[x] < height[y]) {
fathers[x] = y;
}
else {
fathers[x] = y;
height[x]++;
}
}
int main() {
int n;
while (scanf_s("%d", &n)) {
if (n == 0) {
break;
}
else {
for (int i = 0; i < n; i++) {
fathers[i] = i;
height[i] = 0;
}
for (int i = 0; i < n * (n - 1) / 2; i++) {
int b, c, d;
cin >> b >> c >> d;
q.push(edge(b, c, d));
}
int s = 0;
while (q.size() > 1) {
if (find(q.top().from) != find(q.top().to)) {
Union(q.top().from, q.top().to);
s += q.top().weight;
q.pop();
}
else {
q.pop();
}
}
cout << s << endl;
}
}
}
// 64 位输出请用 printf("%lld")
这个警告的意思是,你只重载了<,没有重载>,你要整个一套都重载才行