当前位置:网站首页>prim生成树
prim生成树
2022-08-01 22:42:00 【秦小咩】
# include<iostream>
# include<deque>
# include<vector>
# include<map>
# include<queue>
# include<cstring>
using namespace std;
typedef long long int ll;
ll inf=(1ll<<50);
int n,m;
ll ans;
ll nowdis[5050];
typedef struct
{
int b,e;
ll w;
} xinxi;
xinxi s[200000*3+10];
int len;
int f[200000*3+10];
int nex[200000*3+10];
void add(int x,int y,int z)
{
s[len].b=x;
s[len].e=y;
s[len].w=z;
nex[len]=f[x];
f[x]=len;
len++;
}
void prim()
{
int x=f[1];
for(int i=2;i<=n;i++)
{
nowdis[i]=inf;
}
while(x!=-1)
{
int j=s[x].e;
nowdis[j]=min(nowdis[j],s[x].w);
x=nex[x];
}
for(int i=2; i<=n; i++)
{
ll minn=inf;
int id=0;
for(int j=2; j<=n; j++)
{
if(nowdis[j]<minn&&nowdis[j])
{
minn=nowdis[j];
id=j;
}
}
ans+=minn;
nowdis[id]=0;
int x=f[id];
while(x!=-1)
{
int j=s[x].e;
if(s[x].w<nowdis[j])
{
nowdis[j]=s[x].w;
}
x=nex[x];
}
}
}
int main ()
{
memset(f,-1,sizeof(f));
cin>>n>>m;
for(int i=1; i<=m; i++)
{
int x,y;
ll z;
cin>>x>>y>>z;
add(x,y,z);
add(y,x,z);
}
prim();
for(int i=1;i<=n;i++)
{
if(nowdis[i])
{
cout<<"orz";
return 0;
}
}
cout<<ans;
return 0;
}
边栏推荐
猜你喜欢
03. GO language variable definition, function
Advanced Algebra_Proof_The algebraic multiplicity of any eigenvalue of a matrix is greater than or equal to its geometric multiplicity
小程序毕设作品之微信美食菜谱小程序毕业设计成品(7)中期检查报告
xctf攻防世界 Web高手进阶区 web2
SQL29 Calculate the average next day retention rate of users
Use Jenkins for continuous integration, this knowledge point must be mastered
How to add a game character to a UE4 scene
数据分析04
小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
移动端人脸风格化技术的应用
随机推荐
小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
【移动Web】移动端适配
从0到1:图文投票小程序设计与研发笔记
leetcode 204. Count Primes 计数质数 (Easy)
_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined
NgRx Store createSelector 的单步调试和源代码分析
excel remove all carriage return from a cell
PAM 回文自动机
【Verilog刷题篇】硬件工程师从0到入门1|基础语法入门
excel vertical to horizontal
SOM Network 1: Principles Explained
别看了,这就是你的题呀
深度学习Course2第一周Practical aspects of Deep Learning习题整理
域名重定向工具 —— SwitchHosts 实用教程
数据分析04
2022-08-01 第八组 曹雨 泛型 枚举
杭电多校3 1012. Two Permutations dp*
Implementation principle of VGUgarbage collector (garbage collector)