Jungle Roads(最小生成树、并查集)

不认识的单词:

relent:发慈悲、收敛缓和   relentless:无情

elder:老大

 

 

题目描述:

    The Head Elder of( the tropical island)( of Lagrishan) has a problem.

热带岛的老大有个问题

A burst (of foreign aid money )was spent (on extra roads)( between villages some years ago.)

一个爆发 (外国帮助的钱)花了 在额外的道路(  在乡村之间 ) 

But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain.

但是 这个丛林  无情地 超过 道路  (杂草丛生) ,所以大量的道路网络  太昂贵,以至于无法维持

The Council of Elders must choose to stop maintaining some roads.

这个理事会of老大 必须选择一些 停止维持一些道路

The map( above on the left )shows all the roads (in use now )and the cost (in aacms)( per month to maintain them).

这个图(在左上) 现在现在正在使用的所有的路,和每个月to维持他的成本

Of course ,there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before.

当着,确实需要有一些道路to 联通所有村庄,即使道路不如从前短。

The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages.

首席老大想要告诉理事会of老大   什么是最小的数量(他们能花在道路上,维持全联通)

The villages are labeled A through I in the maps above.

The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month.

Your task is to write a program that will solve such problems.

输入:

    The input consists of one to 100 data sets, followed by a final line containing only 0.

输入 由0-100的数据集组成,最后一行==0

Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized.

每个数据集  刚开始  只有一个n == 村庄的数量   1 < n <= 26,  并且 村庄对应第N个字母 标签化了,大写。

Each data set is completed with n-1 lines that start with village labels in alphabetical order.

There is no line for the last village.

Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet.

If k is greater than 0, the line continues with data for each of the k roads.

The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road.

Maintenance costs will be positive integers less than 100.

All data fields in the row are separated by single blanks.

The road network will always allow travel between all the villages.

The network will never have more than 75 roads.

No village will have more than 15 roads going to other villages (before or after in the alphabet).

In the sample input below, the first data set goes with the map above.

输出:

    The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.

样例输入:

9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0

样例输出:

216
30

来源:

2010年北京大学计算机研究生机试真题

 

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;


typedef struct Edge {
	int u, v;
	int len;
} Edge;


bool compare(Edge& a, Edge& b) {
	return a.len < b.len;
}

class UnionFind {
		int minIndex;
		int maxIndex;
		int cnt;

		int father[101];
		int contain[101];
	public :
		UnionFind() {}
		UnionFind(int minIndex, int maxIndex) {
			this->minIndex = minIndex;
			this->minIndex = minIndex;
			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 {
				int d = father[x];
				int root = getRoot(d);
				if (d != root) {
					father[x] = root;
					contain[d] -= contain[x];
				}
				return root;
			}
		}

		int connect(int x, int y) {
			int xRoot = getRoot(x);
			int yRoot = getRoot(y);
			if (xRoot == yRoot) {
				return 0;
			} else {
				if (contain[xRoot] >= contain[yRoot]) {
					father[yRoot] = xRoot;
					contain[xRoot] += contain[yRoot];
				} else {
					father[yRoot] = xRoot;
					contain[yRoot] += contain[xRoot];
				}
				cnt--;
				return 1;
			}
		}

		int getCnt() {
			return cnt;
		}


		void printVector(vector<Edge> edges) {
			for (int i = 0; i <= edges.size() - 1; i++) {
				cout << edges[i].u << "\t" <<edges[i].v  << "\t" <<edges[i].len << endl;
			}
			
		}

		int mst(vector<Edge> edges) {
			sort(edges.begin(), edges.end(), compare);
		//	printVector(edges);
			int res = 0;
			for (int i = 0; i <= edges.size() - 1; i++) {
				int flag = connect(edges[i].u, edges[i].v);
				if (flag) {
					res += edges[i].len;
					if (getCnt() == 1) {
						return res;
					}
				} else {
					continue;
				}

			}
			return -1;
		}
};



int main() {
	// A 0 / B 1 / .../ Z 25
	int n;// 9个
	while (cin >> n && n != 0) {
		vector<Edge> edges;
		for (int i = 0; i <= n - 2; i++) { // A - H
			int num, u, v, len;
			char ch;
			cin >> ch;
			cin >> num;
			for (int j = 1; j <= num; j++) {
				Edge edge;
				edge.u = i;
				cin >> ch;
				edge.v = ch - 'A';
				cin >> edge.len;
				edges.push_back(edge);
			}
		}

		UnionFind uf(0,n - 1);
		int res = uf.mst(edges);
		cout << res << endl;

	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值