当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)

seaborn笔记:可视化统计关系(散点图、折线图)

(Translation) How the contrasting color of the button guides the user's actions

Prufer序列

2022-08-01 第八组 曹雨 泛型 枚举

域名重定向工具 —— SwitchHosts 实用教程

Postman 批量测试接口详细教程

User Experience | How to Measure User Experience?

一种灵活的智能合约协作方式

小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书
随机推荐
number of solutions to solve a multivariate multi-degree equation
小程序中的多表联合查询
工程建筑行业数据中台指标分析
SAP Spartacus NgExpressEngineDecorator 的工作原理
深度学习Course2第二周Optimization Algorithms习题整理
一种灵活的智能合约协作方式
深度学习Course2第一周Practical aspects of Deep Learning习题整理
leetcode刷题
img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
2022-08-01 第八组 曹雨 泛型 枚举
excel remove all carriage return from a cell
Getting Started Database Days4
SQL29 Calculate the average next day retention rate of users
小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
Yizhou Financial Analysis | The intelligent transformation of bank ATM machines is accelerated; the new Internet loan regulations bring challenges
No more rolls!After joining ByteDance for a week, he ran decisively.
PAM 回文自动机
Use Jenkins for continuous integration, this knowledge point must be mastered
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D 题解
自建 Prometheus 采集腾讯云容器服务监控数据最佳实践