当前位置:网站首页>Minimum spanning tree problem
Minimum spanning tree problem
2022-06-13 00:56:00 【-LM-】

Kruskal Algorithmic solution
#include<bits/stdc++.h>
using namespace std;
const int maxn=5005;
const int maxm=200005;
int n,m;
int x,y,z;
struct edge{
int u,v,w;
};
edge bian[maxm];
int f[maxn];
int ans=0,flag=0,tot=0;
int find(int x)
{
if(x==f[x]) return x;
return f[x]=find(f[x]);
}
bool cmp(edge x,edge y)
{
return x.w<y.w;
}
void kruskal()
{
for(int i=1;i<=m;i++)
{
int u=find(bian[i].u);
int v=find(bian[i].v);
if(u!=v)
{
ans=ans+bian[i].w;
tot++;
f[u]=v;
}
if(tot==n-1){
flag=1; break;
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++) f[i]=i;
for(int i=1;i<=m;i++)
cin>>bian[i].u>>bian[i].v>>bian[i].w;
sort(bian+1,bian+m+1,cmp);
kruskal();
if(flag==0) cout<<"orz";
else cout<<ans;
return 0;
}
Prime Algorithm
#include<bits/stdc++.h>
using namespace std;
#define inf 0x7ffffff
#define maxn 5005
#define maxm 200005
struct edge{
int v,w,next;
}e[maxm*2];
int head[maxn],dis[maxn],cnt,n,m,tot,now=1,ans;
bool vis[maxn];
void add(int u,int v,int w)
{
e[++cnt].v=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
void init()
{
cin>>n>>m;
int u,v,w;
for(int i=1;i<=m;i++)
{
cin>>u>>v>>w;
add(u,v,w); add(v,u,w);
}
}
int prime()
{
for(int i=2;i<=n;i++)
dis[i]=inf;
for(int i=head[1];i;i=e[i].next)
{
dis[e[i].v]=min(dis[e[i].v],e[i].w);
}
while(++tot<n)
{
int minn=inf;
vis[now]=1;
for(int i=1;i<=n;i++)
{
if(!vis[i]&&minn>dis[i])
{
minn=dis[i];
now=i;
}
}
ans+=minn;
for(int i=head[now];i;i=e[i].next)
{
int v=e[i].v;
if(dis[v]>e[i].w&&!vis[v])
dis[v]=e[i].w;
}
}
return ans;
}
int main()
{
init();
cout<<prime()<<"\n";
return 0;
}
边栏推荐
- Rest at home today
- [North Asia server data recovery] data recovery case of Hyper-V service paralysis caused by virtual machine file loss
- AOF持久化
- What is meebits? A brief explanation
- Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing
- Maybe we can figure out the essence of the Internet after the dust falls
- Undirected graph -- computing the degree of a node in compressed storage
- Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
- . The way to prove the effect of throwing exceptions on performance in. Net core
- How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic benefits of ASI, VR, arbr, DPO and trix indicators
猜你喜欢

How to handle different types of data

Pipeline pipeline project construction

Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing

Download nail live playback through packet capturing

Druid reports an error connection holder is null
![[Latex] 插入图片](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[Latex] 插入图片

Development notes of Mongoose

今日睡眠质量记录74分

Self use notes for problem brushing learning

How to solve the duplication problem when MySQL inserts data in batches?
随机推荐
什么是 Meebits?一个简短的解释
How the ET framework uses it to develop games
Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing
(01).NET MAUI实战 建项目
What is meebits? A brief explanation
Opencv desaturation
Androi天氣
草在结种子了
Jenkins持续集成操作
Physical orbit simulation
What is the difference between pytorch and tensorflow?
MySQL locates the position of the character in the string String substitution
How to determine whether T is a value type in a generic type or a reference class- How to determine whether T is a value type or reference class in generic?
Common skills of quantitative investment -- Drawing Part 1: Drawing stock closing price curve and ochl candle chart
Win10 home vs pro vs enterprise vs enterprise LTSC
Traditional machine learning classification model predicts the rise and fall of stock prices
[JS component] browse progress bar
Arduino controls tb6600 driver +42 stepper motor
In / out / inout details of MySQL stored procedures
天津银行周传凯:从 0 到 1,我的分布式数据库落地经验谈