当前位置:网站首页>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;
}
边栏推荐
- Kotlin 协程,job的生命周期
- Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing
- ROS从入门到精通(零) 教程导读
- .net core 抛异常对性能影响的求证之路
- Kotlin 协程挂起函数 suspend 关键字
- [JS component] floating text
- People and gods are angry. Details of Tangshan "mass beating of women incident"
- [JS] solve the problem that removeeventlistener is invalid after the class listening event from new is bound to this
- ImportError: cannot import name &#039; get_ ora_ doc&#039; from partially initialized module
- [JS component] previous queue prompt
猜你喜欢

Quantitative investment traditional index investment decision vs Monte Carlo simulation method

Maybe we can figure out the essence of the Internet after the dust falls

408 true question - division sequence

How many rounds of deep learning training? How many iterations?

人神共愤,唐山“群殴女性事件”细节...

【SCA-CNN 解读】空间与通道注意力:Spatial and Channel-wise Attention

Druid reports an error connection holder is null

Common skills for quantitative investment - drawing 3: drawing the golden section line

通过抓包下载钉钉直播回放

五篇经典好文,值得一看(2)
随机推荐
Four startup modes of kotlin collaboration
MySQL exception: com mysql. jdbc. PacketTooBigException: Packet for query is too large(4223215 > 4194304)
How to handle different types of data
Breadth first search for node editor runtime traversal
. The way to prove the effect of throwing exceptions on performance in. Net core
Common skills for quantitative investment - drawing 3: drawing the golden section line
什么是 dummy change?
Quantitative investment traditional index investment decision vs Monte Carlo simulation method
Basic operations of FreeMarker
sort
OceanBase 雄踞墨天轮2021年度中国数据库魔力象限领导者
Unitywebrequest asynchronous Download
天津银行周传凯:从 0 到 1,我的分布式数据库落地经验谈
Kotlin coroutine withcontext switch thread
Win10 home vs pro vs enterprise vs enterprise LTSC
ROS从入门到精通(零) 教程导读
The scope builder coroutinescope, runblocking and supervisorscope of kotlin collaboration processes run synchronously. How can other collaboration processes not be suspended when the collaboration pro
What is the difference between pytorch and tensorflow?
[imx6ull] video monitoring project (USB camera +ffmepeg)
Kotlin 协程的四种启动模式