当前位置:网站首页>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;
}边栏推荐
- How to predict SQL statement query time?
- 数据库资源负载管理(下篇)
- Code farming essential SQL tuning (Part 1)
- 同学,你听说过MOT吗?
- AI function of cutting-edge technology exploration: slow SQL discovery
- CLP information - financial standardization "14th five year plan" highlights data security
- Docker uses redis image
- DHCP协议实例化分析
- PostgreSQL source code compilation
- From digital twinning to digital immortality, the "three-stage theory" of the development of the meta universe
猜你喜欢

Code farming essential SQL tuning (Part 2)

Go language slice

Cloud data management will break the island of storage and the island of team

基于FPGA的VGA协议实现

再聊数据中心网络

Hands on, how should selenium deal with pseudo elements?

Memory optimization table mot management

Detailed explanation of opengauss multi thread architecture startup process

推开混合云市场大门,Lenovo xCloud的破局之道

From digital twinning to digital immortality, the "three-stage theory" of the development of the meta universe
随机推荐
Opengauss AI capability upgrade to create a new AI native database
Maui introductory tutorial series (1. framework introduction)
The third generation Pentium B70 won the C-NCAP five-star safety performance again
With a lamp inserted in the nostril, the IQ has risen and become popular in Silicon Valley. 30000 yuan is enough
Overview and example analysis of opengauss database performance tuning
What happened to the frequent disconnection of the computer at home
Import data: GS_ restore or MERGE INTO? See which one suits you better
数据库资源负载管理(下篇)
Export configuration to FTP or TFTP server
Learn how to parse SQL from kernel code
用户界面之工具栏详解-AutoRunner自动化测试工具
一文教会你数据库系统调优
Code farming essential SQL tuning (Part 2)
Cloud data management will break the island of storage and the island of team
PostgreSQL startup process
Discussion on opengauss parallel decoding
[digital signal processing] correlation function (correlation function property | conjugate symmetry property of correlation function | even symmetry of real signal autocorrelation function | conjugat
GO语言-Slice切片
三千字教你使用MOT
收藏 |彻底搞懂感受野的含义与计算