当前位置:网站首页>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;
}
边栏推荐
- (Translation) How the contrasting color of the button guides the user's actions
- 使用Jenkins做持续集成,这个知识点必须要掌握
- 【Verilog刷题篇】硬件工程师从0到入门1|基础语法入门
- 使用 Zokrates 在 BSV 上创建您的第一个 zkSNARK 证明
- 美赞臣EDI 940仓库装运订单详解
- 使用分类权重解决数据不平衡的问题
- ping no reply
- 基于 OData 模型和 JSON 模型的 SAP UI5 表格控件行项目的添加和删除实现
- excel change cell size
- How to prevent governance attacks in DAOs?
猜你喜欢

The must-have "fishing artifact" for programmers is here!

小程序中的多表联合查询

APP special test: traffic test

数据分析04

03. GO language variable definition, function

User Experience | How to Measure User Experience?

从0到1:图文投票小程序设计与研发笔记

游戏元宇宙发展趋势展望分析

Postman batch test interface detailed tutorial

小程序容器+自定义插件,可实现混合App快速开发
随机推荐
JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法
Prufer序列
别看了,这就是你的题呀
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D 题解
【C补充】链表专题 - 单向链表
APP special test: traffic test
Today's sleep quality record 74 points
RxJs SwitchMapTo 操作符之移花接木
xctf attack and defense world web master advanced area web2
【好书推荐】第一本无人驾驶技术书
基于 OData 模型和 JSON 模型的 SAP UI5 表格控件行项目的添加和删除实现
Safe fifth after-school exercise
excel remove all carriage return from a cell
visual studio code multiple editing
[Recommended books] The first self-driving technology book
seaborn笔记:可视化统计关系(散点图、折线图)
如何给 UE4 场景添加游戏角色
excel edit a cell without double clicking
一种灵活的智能合约协作方式
【移动Web】移动端适配