当前位置:网站首页>09 Minimum Spanning Tree highway

09 Minimum Spanning Tree highway

2022-06-11 16:03:00 Tiansheng moon

In the statistical data table of existing inter village roads , It lists the cost of several roads that are likely to be built into standard roads , Every village needs the lowest cost of connectivity .

Input format :

The input data includes a positive integer for the number of towns N(≤1000) And the number of candidate roads M(≤3N); And then M Row correspondence M road , Each line gives 3 A positive integer , They are the number of the two towns directly connected by the road and the estimated cost of the road reconstruction . For the sake of simplicity , Town from 1 To N Number .

Output format :

The minimum cost of exporting village to village access . If the input data is not enough to ensure smooth flow , The output −1, It means more roads need to be built .

sample input :

6 15
1 2 5
1 3 3
1 4 7
1 5 4
1 6 2
2 3 4
2 4 6
2 5 2
2 6 6
3 4 6
3 5 1
3 6 1
4 5 10
4 6 8
5 6 3

sample output :

12

Code length limit

16 KB

The time limit

400 ms

Memory limit

64 MB

#include<iostream>
#include<cstdlib>
#define P 1005
#define inf 9999999
using namespace std;

typedef struct ENode * PtrToENode;
struct ENode{
    int V1,V2;
    int Weight;
};
typedef PtrToENode Edge;

typedef struct AdjVNode * PtrToAdjVNode;
struct AdjVNode{
    int Adjv;// Adjacency subscript 
    int Weight;// Edge weight 
    PtrToAdjVNode Next;
};

typedef struct Vnode{
    PtrToAdjVNode FirstEdge;
    int Data;
}AdjList[P];

typedef struct Gnode * PtrToGnode;
struct Gnode{
    int Nv;
    int Ne;
    AdjList G;
};
typedef PtrToGnode LGraph;

struct Graph{
    int Nv;// Number of vertices 
    int Ne;// Number of edges 
    int G[P][P];// Adjacency matrix 
};

void InsertEdge(LGraph Graph,Edge E)
{
    PtrToAdjVNode NewNode;
    // Insert edge <V1,V2>
    // by V2 Create a new node 
    NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
    NewNode->Adjv = E->V2;
    NewNode->Weight = E->Weight;
    // take V2 Insert V1 The header 
    NewNode->Next = Graph->G[E->V1].FirstEdge;
    Graph->G[E->V1].FirstEdge = NewNode;
    
    // by V1 Create a new node 
    NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
    NewNode->Adjv = E->V1;
    NewNode->Weight = E->Weight;
    // take V1 Insert V2 The header 
    NewNode->Next = Graph->G[E->V2].FirstEdge;
    Graph->G[E->V2].FirstEdge = NewNode;
}

int FindMinDist(struct Graph *Graph,int dist[])
{
    int MinV,V;
    int MinDist = inf;
    
    for(V = 1; V <= Graph->Nv;V++)
    {
        if(dist[V] != 0 && dist[V] < MinDist)
        {
            MinDist = dist[V];
            MinV = V;
        }
    }
    
    if(MinDist < inf)
        return MinV;
    else
        return -1;
}

int main()
{
    struct Graph *Graph;
    Edge E;
    LGraph MST;
    Graph = (struct Graph *)malloc(sizeof(struct Graph));
    cin >> Graph->Nv >> Graph->Ne;
    int i,j,x,y,z;
    int V,W;
    int dist[P];// The vertices i To Vt The minimum weight of 
    int parent[P];
    int TotalWeight = 0;// Weight and 
    int VCount = 0;// Number of recorded vertices 
    // Initialize to no path 
    
    for(i = 1;i <= Graph->Nv;i++)
    {
        for(j = 1;j <= Graph->Nv;j++)
        {
            Graph->G[i][j] = inf;
        }
    }
    // assignment 
    for(i = 1;i <= Graph->Ne;i++)
    {
        cin >> x >> y >> z;
        Graph->G[x][y] = Graph->G[y][x] = z;
    }
    
    for(V = 1; V <= Graph->Nv;V++)
    {
        dist[V] = Graph->G[1][V];
        parent[V] = 1;
    }
    
    MST = (LGraph)malloc(sizeof(struct Gnode));
    MST->Nv = Graph->Nv;
    MST->Ne = 1;
    for(int V = 1; V <= MST->Nv;V++)
    {
        MST->G[V].FirstEdge = NULL;
    }
    E = (Edge)malloc(sizeof(struct ENode));
    
    dist[1] = 0;
    VCount++;
    parent[1] = -1;
    
    while(1)
    {
        V = FindMinDist(Graph,dist);
        if(V == -1)
        {
            break;
        }
        E->V1 = parent[V];
        E->V2 = V;
        E->Weight = dist[V];
        InsertEdge(MST,E);
        TotalWeight += dist[V];
        dist[V] = 0;
        VCount++;
        
        for(W = 1;W <= Graph->Nv;W++)// For each vertex in the graph w
        {
            if(dist[W] != 0 && Graph->G[V][W] < inf)
            {
                if(Graph->G[V][W] < dist[W])
                {
                    dist[W] = Graph->G[V][W];
                    parent[W] = V;
                }
            }
        }
    }
    if(VCount < Graph->Nv)
        TotalWeight = -1;
    cout << TotalWeight << endl;
    
    return 0;
}

原网站

版权声明
本文为[Tiansheng moon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111546388420.html