Problem Description 调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。“畅通工程”的目标是使任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
Input 测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
Output 对每个测试用例,在1行里输出最小的公路总长度。
Sample Input
Sample Output
|
/*
oj提示Runtime Error(ACCESS_VIOLATION),后来修改后,A了。
原来是n = 1 只有一个村,则需要输出0:不需要建路,但是我卡了,甚至还考虑是vector的问题,改成数组版本的了,唉
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <stack>
#include <iomanip>
using namespace std;
typedef struct Edge {
int u, v;
int len; // 长度 距离
} Edge;
vector<Edge> edges; // 边
bool compare(Edge& a, Edge& b) {
return a.len <= b.len;
}
class UnionFind {
int father[1001]; // 1 ~ 100 记录上级/老大
int contain[1001]; // 包含多少小弟
int minIndex; // 范围
int maxIndex; // 范围
int cnt; //连通分量的个数
public :
UnionFind() {}
UnionFind(int minIndex, int maxIndex) {
// 1 ~ 4
this->minIndex = minIndex;
this->maxIndex = maxIndex;
this->cnt = maxIndex - minIndex + 1; // 连通分量的个数
for (int i = minIndex; i <= maxIndex ; i++) {
father[i] = -1;
contain[i] = 1;
}
}
int getRoot(int x) {
if (father[x] == -1) {
return x; // 自己就是老大
} else {
//自己不是老大,自己有上级
// 上级是set老大 还是 图上的父节点?
int d = father[x];
int root = getRoot(d);
if (d != root) {
father[x] = root; // 直接跟root算了,不跟图上的父节点
contain[d] -= contain[x]; // contain少了,投奔root了
}
return root;
}
}
bool isConnected(int x, int y) {
return getRoot(x) == getRoot(y);
}
bool connect(int x, int y) {
int xRoot = getRoot(x);
int yRoot = getRoot(y);
if (xRoot == yRoot) {
//cout << "已经在同一个set里面了" << endl;
return false; // 已经在同一个set里面了,已经在同一个连通分量里面了
} else {
if(contain[x] >= contain[y]) {
// x 人多势大, y投奔x
father[yRoot] = xRoot;
contain[xRoot] += contain[yRoot];
} else {
// y人多势大,x投奔y
father[xRoot] = yRoot;
contain[yRoot] += contain[xRoot];
}
}
cnt --; //连通分量少1
}
int getCnt() {
return cnt;
}
void show() {
cout << "---目前情况---" << endl;
cout << "father :";
for (int i = minIndex; i <= maxIndex; i++) {
cout << setw(4) << father[i] << "\t";
}
cout << endl;
cout << "index :";
for (int i = minIndex; i <= maxIndex; i++) {
cout << setw(4) << i << "\t";
}
cout << endl << endl;
cout << "contain :";
for (int i = minIndex; i <= maxIndex; i++) {
cout << setw(4) << contain[i] << "\t";
}
cout << endl;
cout << "index :";
for (int i = minIndex; i <= maxIndex; i++) {
cout << setw(4) << i << "\t";
}
cout << endl;
cout << "---打印完毕---" << endl << endl;
}
int mst(vector<Edge> edges) {
int sum = 0;
sort(edges.begin(), edges.end(), compare);
for (int i = 0; i <= edges.size() - 1; i++) {
bool flag = connect(edges[i].u, edges[i].v);
if (flag) {
sum += edges[i].len;
if (getCnt() == 1) {
return sum;
}
} else {
continue;
}
}
return -1;
}
};
int main() {
int n;
while (cin >> n && n != 0) {
if (n == 1) {
cout << "0" << endl;
} else {
edges.clear();
UnionFind uf(1,n);
int caseNum = n * (n - 1) / 2;
for (int i = 0; i <= caseNum - 1; i ++) {
Edge edge;
cin >> edge.u >> edge.v >> edge.len;
edges.push_back(edge);
}
int sum = uf.mst(edges);
cout << sum << endl;
}
}
}