还是畅通工程(最小生成树)(kruskal算法)(结构体的构造函数)

Problem Description

调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。“畅通工程”的目标是使任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。

 

 

Input

测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
当N为0时,输入结束,该用例不被处理。

 

 

Output

对每个测试用例,在1行里输出最小的公路总长度。

 

 

Sample Input

3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0

 

 

Sample Output

3
5

 

 

/*
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;
		}
	}
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值