书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

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.

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
 
首先,先要搞懂这个题目的意思.
前头的输入就求说了.来看看
1 2
是什么意思吧.
它的意思是说,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.  阅读(475)  评论(0编辑  收藏  举报

导航

书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!