PKU2421 -一道不错的题(用到了并查集,优先队列)

Description

There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.
 

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.

Input

The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.
 

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

Output

You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum. 代码实现如下: /* 很好,用上了优先队列 和 并查集 两种数据结构 */ #include <iostream> #include <queue> #include <vector> #include <set> #define MAX 105 using namespace std; int matrix[MAX][MAX]; //优先队列 struct node { int a, b; int distance; node(int ta, int tb, int tdistance) { a = ta; b = tb; distance = tdistance; } }; struct cmp { bool operator()(const node& a, const node& b) { return a.distance > b.distance; } }; priority_queue<node, vector<node>, cmp> m_queue; //并查集 int parent[MAX], rank[MAX]; void initial(int n) { int i, j; for(i = 1; i<= n; i ++) { parent[i] = i; rank[i] = 1; } } int find(int num) { if(parent[num] != num) { parent[num] = find(parent[num]); } return parent[num]; } void merge(int a, int b) { int tmpa = find(a); int tmpb = find(b); if(rank[tmpa] > rank[tmpb]) { parent[tmpb] = tmpa; } else if(rank[tmpa] < rank[tmpb]) { parent[tmpa] = tmpb; } else { parent[tmpa] = tmpb; rank[tmpb] ++; } } int main() { int n, i, j, q, a, b; //输入矩阵,同时用矩阵初始化优先队列m_queue cin >> n; for(i = 1; i <= n; i++) for(j = 1; j<= n; j++) cin >> matrix[i][j]; for(i = 1; i<= n; i++) { for(j = i+1; j<= n;j++) { node tmp(i, j, matrix[i][j]); m_queue.push(tmp); } } //输入已经建立连接的村庄,通过并查集来,实现集合的合并 cin >>q; initial(n); for(i = 0; i< q; i++) { cin >> a >> b; int tmpa = find(a); int tmpb = find(b); if(tmpa != tmpb) merge(tmpa, tmpb); } //判断是否通过上面的连接输入之后,是否还需要别的边的加入 //如果num ==1 说明各个村庄已经是互联的了,如果num >1 说明还需要加入边 set<int> mset; for(i = 1; i<= n; i++) { mset.insert(parent[i]); } int num = mset.size(); //通过优先队列的输出,来找到需要加入最小的边的总和result int result = 0; while(num > 1 && !m_queue.empty()) { node tmp = m_queue.top(); m_queue.pop(); int tmpa = find(tmp.a); int tmpb = find(tmp.b); if(tmpa != tmpb) { num –; result += tmp.distance; merge(tmpa, tmpb); } } cout << result << endl; return 0; }

    原文作者:犯罪团伙问题
    原文地址: https://blog.csdn.net/suwei19870312/article/details/5396597
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞