当前位置:网站首页>minimum spanning tree
minimum spanning tree
2022-07-01 13:15:00 【jigsaw_ zyx】
List of articles
Plain version prim Algorithm
key word : A dense picture , Time complexity O(n^2)
Each time, select a point that is not at the minimum distance of the connected block , Then update the data
Title Description
Given a n A little bit m Striped Undirected graph , There may be Double edge and self ring , The edge weight may be negative .
Find the sum of tree edge weights of the minimum spanning tree , If the minimum spanning tree does not exist, output impossible.
Given an undirected graph with weights G=(V, E), among V Represents the set of points in the graph ,E Represents a set of edges in a graph ,n=|V|,m=|E|.
from V All of n A vertex and E in n-1 An undirected connected subgraph composed of edges is called G A spanning tree of , The spanning tree in which the sum of the weights of the edges is the smallest is called an undirected graph G The minimum spanning tree of .
Input format
The first line contains two integers n and m.
Next m That's ok , Each line contains three integers u,v,w, Indication point u Sum point v There is a weight of w The edge of .
Output format
All in one line , If there is a minimum spanning tree , Then an integer is output , Represents the sum of tree edge weights of the minimum spanning tree , If the minimum spanning tree does not exist, output impossible.
Data range
1≤n≤5001≤n≤500,
1≤m≤1051≤m≤105,
The absolute values of the edge weights of the edges involved in the graph do not exceed 10000.
sample input :
4 5
1 2 1
1 3 2
1 4 3
2 3 2
3 4 4
sample output :
6
Code
#include<bits/stdc++.h>
using namespace std;
// A dense picture
const int N=510,M=10010,INF=0x3f3f3f3f;
int n,m;
int g[N][N];
int d[N];
bool v[N];
int prim(){
memset(d,0x3f,sizeof d);
int res=0;
for(int i=0;i<n;i++){
int t=-1;
for(int j=1;j<=n;j++)
if(!v[j]&&(t==-1||d[t]<d[j]))
t=j;
if(i&&d[t]==INF) return -1;// Cannot generate minimum spanning tree
v[t]=true;
if(i) res+=d[t];
for(int j=1;j<=n;j++)
d[j]=min(d[j],g[t][j]);// Update distance from point to set
}
return res;
}
int main(){
cin>>n>>m;
memset(g,0x3f,sizeof g);
while(m--){
int u,v,w;
cin>>u>>v>>w;
g[u][v]=g[v][u]=min(g[u][v],w);
}
int t=prim();
if(t==-1) cout<<"impossible"<<endl;
else cout<<t<<endl;
return 0;
}
边栏推荐
- MySQL statistical bill information (Part 2): data import and query
- Apache-Atlas-2.2.0 独立编译部署
- Yarn重启applications记录恢复
- CS5268优势替代AG9321MCQ Typec多合一扩展坞方案
- 启动solr报错The stack size specified is too small,Specify at least 328k
- 1553B environment construction
- Update a piece of data from the database. Will CDC get two pieces of data with OP fields D and C at the same time? I remember before, only OP was U
- Run PowerShell script prompt "because running script is prohibited on this system" solution
- Quickly understand what the compressed list in redis is
- 科学创业三问:关于时机、痛点与重要决策
猜你喜欢
Reasons for MySQL reporting 1040too many connections and Solutions
图灵奖得主Judea Pearl:最近值得一读的19篇因果推断论文
【大型电商项目开发】性能压测-压力测试基本概念&JMeter-38
nexus搭建npm依赖私库
Application of 5g industrial gateway in scientific and technological overload control; off-site joint law enforcement for over limit, overweight and overspeed
我选的热门专业,四年后成了“天坑”
Cs5268 advantages replace ag9321mcq typec multi in one docking station scheme
Svg diamond style code
shell脚本导入存储过程到数据库
The popular major I chose became "Tiankeng" four years later
随机推荐
CV顶会最佳论文得主分享:好论文是怎么炼成的?
Wang Xing's infinite game ushers in the "ultimate" battle
R language builds a binary classification model based on H2O package: using H2O GBM build gradient hoist model GBM, use H2O AUC value of AUC calculation model
北斗通信模块 北斗gps模块 北斗通信终端DTU
Vs code set code auto save
Function test process in software testing
Report on the 14th five year plan and future development trend of China's integrated circuit packaging industry Ⓓ 2022 ~ 2028
启动solr报错The stack size specified is too small,Specify at least 328k
Use Net core access wechat official account development
Jenkins+webhooks- multi branch parametric construction-
Global and Chinese styrene acrylic lotion polymer development trend and prospect scale prediction report Ⓒ 2022 ~ 2028
Run PowerShell script prompt "because running script is prohibited on this system" solution
声明一个抽象类Vehicle,它包含私有变量numOfWheels和公共函数Vehicle(int)、Horn()、setNumOfWheels(int)和getNumOfWheels()。子类Mot
Nexus builds NPM dependent private database
一款Flutter版的记事本
SQLAlchemy在删除有外键约束的记录时,外键约束未起作用,何解?
Has anyone ever encountered this situation? When Oracle logminer is synchronized, the value of CLOB field is lost
How can genetic testing help patients fight disease?
Flutter SQLite使用
Example code of second kill based on MySQL optimistic lock