poj 2421 Constructing Roads 并查集+最小生成树
2014-08-04 09:35
423 查看
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.
Sample Input
Sample Output
179
题意及分析:
这个是上篇的那个题(poj2031)的简化。有n个村庄,已存在m条路,现在修一些其它的路,是村庄之间两两联通,并且路的总长度最短。具体过程可以参见上篇题解,就不多说了。
AC代码:
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.
Sample Input
3 0 990 692 990 0 179 692 179 0 1 1 2
Sample Output
179
题意及分析:
这个是上篇的那个题(poj2031)的简化。有n个村庄,已存在m条路,现在修一些其它的路,是村庄之间两两联通,并且路的总长度最短。具体过程可以参见上篇题解,就不多说了。
AC代码:
相关文章推荐
- poj 2421 Constructing Roads prim最小生成树 基础!!!
- poj 2421(最小生成树)
- Poj 2421 Constructing Roads(Prim 最小生成树)
- poj-2421 Constructing Roads(最小生成树 Kruskal算法)
- POJ2421_Constructing Roads(最小生成树)
- poj 2421 最小生成树(prime) 模版题
- POJ 2421 图的最小生成树 prim算法 模板代码的利用
- Poj 2421 Constructing Roads(Prim 最小生成树)
- (kruscal12.1.1)POJ 2421 Constructing Roads(使用kruscal算法来生成最小生成树&&计算最小带权路径和)
- POJ-2421 最小生成树 算法复习计划
- poj 1258 并查集的基本应用 最小生成树
- poj1861最小生成树(并查集)-kruskal算法
- POJ 2421 Constructing Roads(最小生成树)
- POJ 2421 Constructing Roads 最小生成树 kruscal+并查集
- POJ 1861 Network [最小生成树算法MST-kruskal 数据结构-并查集 union-find sets]
- poj 1789 最小生成树 kruskal 并查集
- 最小生成树&并查集 POJ 1861 Network
- 最小生成树&并查集POJ 1287 Networking
- POJ - 1251 Jungle Roads (最小生成树Kruskal、并查集)
- poj2421 最小生成树 克鲁斯