算法学习【8】——1083. Networking

题目:http://soj.me/1083

1083. Networking

Description

You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area.

Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

Input

The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line.

The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.

Output

For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

Sample Input

1 0

2 3
1 2 37
2 1 17
1 2 68

3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32

5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12

0

Sample Output

0
17
16
26

思路:最小生成树,有重复边,无限边,没了。其实用Prim会好点,因为在邻接矩阵空间不大,而且,可以消除大量重复的边。不过之前实现的Prim不是邻接矩阵的,就懒得改了。直接用Kruskal类(http://blog.csdn.net/betabin/article/details/7389263)改下去实现了。

代码如下:

// Problem#: 1083
// Submission#: 1302232
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include 
#include 
#include 

#define MAX_VERTEX 53

using namespace std;

struct Edge 
{
    int source;
    int destination;
    int cost;
};

bool operator < (const Edge &a, const Edge &b)
{
    return a.cost > b.cost;
}

priority_queue kruskalQueue;          //Save all edges.
//bool inMap[MAX_VERTEX][MAX_VERTEX];

class Graph
{
public:
    int allCost;
    int vertexNum;
    int edgeNum;

    void kruskal();
    void initialize();

private:
    int vertexRoot[MAX_VERTEX];                 //Save the root of the tree the node belong to.
    //Sometimes it just save node's father.

    void getData();

    void rootReset();                           //Reset all root of each vertex.
    void refreshRoot(int node, int root);       //Update the root of the node and its ancestors.
    int findRoot(int node);                     //Find the root of the tree the node belong to.
    void unionTree(int nodeA, int nodeB);       //Set nodeB as nodeA's father.
};

void Graph::rootReset()
{
    for (int i = 0; i < vertexNum; i++)
    {
        vertexRoot[i] = i;
    }
}

void Graph::initialize()
{
    allCost = 0;
    vertexNum = 0;
    edgeNum = 0;

    while (!kruskalQueue.empty())
    {
        kruskalQueue.pop();
    }

    getData();
    rootReset();
}

void Graph::getData()
{
    scanf("%d", &vertexNum);

    if (0 == vertexNum)
    {
        return ;
    }
    
    scanf("%d", &edgeNum);

    Edge inputEdge;
    for (int i = 0; i < edgeNum; i++)
    {
        scanf("%d %d %d", &inputEdge.source, &inputEdge.destination, &inputEdge.cost);
        inputEdge.source--;
        inputEdge.destination--;
        kruskalQueue.push(inputEdge);
    }
}

void Graph::refreshRoot(int node, int root)
{
    int father;

    while (root != vertexRoot[node])
    {
        father = vertexRoot[node];
        vertexRoot[node] = root;
        node = father;
    }
}

int Graph::findRoot(int node)
{
    int rootNode = node;
    while (vertexRoot[rootNode] != rootNode)
    {
        rootNode = vertexRoot[rootNode];
    }

    refreshRoot(node, rootNode);

    return rootNode;
}

void Graph::unionTree(int nodeA, int nodeB)
{
    vertexRoot[vertexRoot[nodeA]] = vertexRoot[nodeB];
}

void Graph::kruskal()
{
    int gotEdgeNum = 0;
    int edgeNeeded = vertexNum - 1;
    Edge currentEdge;

    while (!kruskalQueue.empty() && gotEdgeNum < edgeNeeded)
    {
        currentEdge = kruskalQueue.top();
        kruskalQueue.pop();

        if (findRoot(currentEdge.source) != findRoot(currentEdge.destination))
        {
            unionTree(currentEdge.source, currentEdge.destination);
            allCost += currentEdge.cost;
            gotEdgeNum++;
        }
    }
}

int main()
{
    Graph test;

    while (true)
    {
        test.initialize();

        if (0 == test.vertexNum)
        {
            break;
        }
        
        test.kruskal();

        printf("%d\n", test.allCost);
    }

    return 0;
}                                 


你可能感兴趣的:(Sicily)