hdoj1102-Constructing Roads(Kruskal)

题目链接-hdoj1102

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

思路:

1.本题题意大概是给出每两个村落直接的距离,然后有些村落之间的道路是修好的,然后求还要修多长的路才能使每两个村落之间都能连接。

2.本题主要是求最小生成树,但是他的输入数据是个邻接矩阵,而且存在部分村落之间的道路已经修好,因此我们可以假设距离为0。

3,首先是读入矩阵数据,然后再将两个村落之间已修好道路的设为0,最后再转化为边,用kruskal方法求出最小生成树,结果即为要修的路的长度。

代码:

//Exe.Time  Exe.Memory
// 78MS      1908K
#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;

struct Edge
{
    int v, u, w;
};

Edge e[5010];
int n;
int father[105];
int data[105][105];

int cmp(Edge e1, Edge e2)
{
    return e1.w <= e2.w;
}

//初始化并查集
void init()
{
    for(int i = 1; i <= n; ++ i)
    {
        father[i] = i;
    }
}

//找出根节点,并按路径优化
int find(int node)
{
    int x = node;
    if(x != father[x])
    {
        father[x] = find(father[x]);
    }
    return father[x];
}

void unit(int x, int y)
{
    int x0 = find(x);
    int y0 = find(y);
    father[y0] = x0;
}

int kruskal()
{
    int total_weight = 0;
    init();
    int cnt = n * (n - 1) / 2;
    sort(e, e + cnt, cmp);
    int count = 0;

    for(int i = 0; i < cnt; ++ i)
    {
        int x = find(e[i].v);
        int y = find(e[i].u);
        if(x != y)
        {
            total_weight += e[i].w;
            unit(x, y);
            count ++;
        }
        if(count == n - 1)
        {
            break;
        }
    }
    return total_weight;
}

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    //ifstream cin("data.in");

    while(cin >> n)
    {
        int total_weight = 0;
        //读入邻接矩阵
        for(int i = 1; i <= n; ++ i)
        {
            for(int j = 1; j <= n; ++ j)
            {
                cin >> data[i][j];
            }
        }
        int q;
        cin >> q;
        while(q --)
        {
            //将两个村落之间存在道路的距离设为0
            int x, y;
            cin >> x >> y;
            data[x][y] = 0;
            data[y][x] = 0;
        }

        int  eCnt = 0;

        for(int i = 1; i <= n; ++ i)
        {
            for(int j = 1; j < i; ++ j)
            {
                //转化为边
                e[eCnt].v = i;
                e[eCnt].u = j;
                e[eCnt++].w = data[i][j];
            }
        }

        total_weight = kruskal();
        cout << total_weight << endl;
    }

    return 0;
} 
原文地址:https://www.cnblogs.com/topk/p/6580109.html