当前位置:网站首页>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;
}边栏推荐
- [Yugong series] June 2022 Net architecture class 077 distributed middleware schedulemaster loading assembly timing task
- 【愚公系列】2022年06月 .NET架构班 076-分布式中间件 ScheduleMaster的执行原理
- Kill the swagger UI. This artifact is better and more efficient!
- How does the taskbar under the computer display open programs
- Database resource load management (Part 1)
- Will you be punished for not wearing seat belts in the back row?
- openGauss 3.0.0版本正式发布,立即体验社区首个轻量版本
- Nat Common | le Modèle linguistique peut apprendre des distributions moléculaires complexes
- 数据库资源负载管理(下篇)
- Verification code is the natural enemy of automation? Ali developed a solution
猜你喜欢

Factory calibrated gravity: working principle and installation position of carbon monoxide sensor, calibration standard description

从屡遭拒稿到90后助理教授,罗格斯大学王灏:好奇心驱使我不断探索

Using cloud DB to build app quick start - quick application

干掉 Swagger UI,这款神器更好用、更高效!

收藏 | 可解释机器学习发展和常见方法!

Princeton Dengjia student's personal account: must I have a doctorate? No, I can also be an applied scientist in a large factory as an undergraduate

AGC安全规则是如何简化用户授权和验证请求

内存优化表MOT管理

【愚公系列】2022年06月 .NET架构班 078-分布式中间件 ScheduleMaster的Worker集群

网站上的 breadcrumb 使用场景浅析
随机推荐
After nine years of testing, the salary for interviewing Huawei is 10000. Huawei employees: the company doesn't have such a low salary position
Using cloud DB to build apps quick start - quick games
Nielseniq announces appointment of Tracey Massey as chief operating officer
Analysis of the execution process of opengauss simple query SQL
数据库资源负载管理(下篇)
WGet command use
PostgreSQL create database
09-最小生成树 公路村村通
[0006] title, keyword and page description
Opengauss database flashback function verification
Overview and example analysis of opengauss database performance tuning
Golang map basic operations
Hands on, how should selenium deal with pseudo elements?
让快递快到来不及退款的,真的不是人
向数据库导入数据?试试COPY FROM STDIN语句
openGauss 3.0.0版本正式发布,立即体验社区首个轻量版本
Maui introductory tutorial series (1. framework introduction)
AI tool for cutting-edge technology exploration: analog detection
It's really not human to let the express delivery arrive before the refund
Will you be punished for not wearing seat belts in the back row?