HDU 1102 Constructing Roads(最小生成樹 Kruskal算法)

原題

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)



Problem 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

題意

輸入一個鄰接矩陣來表示無向圖,再輸入一些路徑表示已連通,你要再加入一些最短的路徑路徑使得該圖爲連通圖,輸出這些路徑的長度和。

涉及知識及算法

求最小生成樹的Kruskal算法(加邊法),用並查集來判斷是否有通路,利用#<algorithm>的sort函數對邊進行排序。

代碼

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAX_N=110;
const int MAX_Q=MAX_N*MAX_N;
int N,Q;
int p[MAX_N];

struct node
{
    int u,v,w;
};
node edge[MAX_Q];

bool cmp(node a,node b)
{
    return a.w<=b.w;
}

int Find(int x)
{
    return p[x]==x?x:Find(p[x]);
}

int Kruskal(int m)
{
    int ans=0;
    int i,x,y;
    //對邊進行排序
    sort(edge,edge+m,cmp);
    for(i=0;i<m;i++)
    {
        x=edge[i].u;
        y=edge[i].v;
        x=Find(x);
        y=Find(y);
        //如果x和y之間沒有通路
        if(x!=y)
        {
            p[x]=y;
            ans+=edge[i].w;
        }
    }
    return ans;
}

int main()
{
    int k,dis,a,b;
    while(cin>>N)
    {
        memset(edge,0,sizeof(edge));
        k=0;
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                cin>>dis;
                //只處理上三角矩陣
                if(i>=j)
                {
                    continue;
                }
                edge[k].u=i;
                edge[k].v=j;
                edge[k].w=dis;
                k++;
            }
        }
        //並查集的初始化
        for(int i=0;i<N;i++)
        {
            p[i]=i;
        }
        cin>>Q;
        for(int i=0;i<Q;i++)
        {
            cin>>a>>b;
            a--,b--;
            //加入爲一個集合(一條邊爲一個集合)
            a=Find(a);
            b=Find(b);
            p[a]=b;
        }
        cout<<Kruskal(k)<<endl;
    }
    return 0;
}
代碼轉載自HDUOJ用戶csc12,向他表示感謝。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章