Kruskal算法解決POJ 2421--有點難度
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.
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.
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
首先,先要搞懂這個題目的意思.
前頭的輸入就求說了.來看看
1
1?。?/span>
是什么意思吧.
它的意思是說,1 2這兩點間的道路是有了.
那末,我們要做的工作就是在剩下的那些點中
找出一條最短的路徑,這個就是Kruskal路徑,
要使得每個村莊之間都要連通。
只要把建好的路恰當的處理一下,這個題目
就非常簡單了,就是純粹的Kurskal算法。
View Code
#include "iostream" #include "algorithm" using namespace std; structEdge { int begin; int end; int length; }; Edge Num[10001]; int Father[1001], Map[1001][1001]; int Minlen, Edge_Num,i, j,TmpBegin, TmpEnd, counter, Total, BuildNum, From, To; int Find(int k) { return Father[k]==k?k:Father[k]=Find(Father[k]); } int Cmp(Edge a, Edge b) { return a.length<b.length; } int Kruskal() { Minlen = 0; Edge_Num = (Total-1)*Total/2; for(i=0; i<Edge_Num; i++) { TmpBegin = Find(Num[i].begin); TmpEnd = Find(Num[i].end); if(TmpBegin != TmpEnd) { Father[TmpBegin] = TmpEnd; Minlen += Num[i].length; } } return Minlen; } void Init() { cin>>Total; counter = 0; Edge_Num = (Total-1)*Total/2; for(i=1; i<=Total; i++) { for(j=1; j<=Total; j++) { cin>>Map[i][j]; if(j>i) { Num[counter].begin = i; Num[counter].end = j; Num[counter++].length = Map[i][j]; } } Father[i] = i; } cin>>BuildNum; while(BuildNum--) { cin>>From>>To; TmpBegin = Find(From); //1 TmpEnd = Find(To); //2 Father[TmpBegin] = TmpEnd; //3 這個地方就是將已經存在的邊用并查集給合并起來,這就是本題的關鍵了。 //標有1 2 3的這三行就是用來處理已經建好的道路的 } sort(Num, Num+counter, Cmp); } int main() { Init(); cout<<Kruskal()<<endl; return 0; }
posted on 2011-09-22 19:10 More study needed. 閱讀(482) 評論(0) 收藏 舉報

浙公網安備 33010602011771號