当前位置:网站首页>Prim求最小生成树(朴素版稠密图)
Prim求最小生成树(朴素版稠密图)
2022-06-13 10:57:00 【重生之我会拧瓶盖】
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 510,INF = 0x3f3f3f3f;
int g[N][N],dist[N];
bool st[N];
int n,m;
int prim()
{
memset(dist,0x3f,sizeof dist);
int res=0;
for (int i = 0; i < n; i ++ )
{
int t=-1;
for (int j = 1; j <= n; j ++ )
if (!st[j]&&(t==-1||dist[t]>dist[j]))
t = j;
if (i&&dist[t]==INF) return INF;
if (i) res+=dist[t];
st[t] = true;
for (int j = 1; j <= n; j ++ )
dist[j] = min(dist[j],g[t][j]);
}
return res;
}
int main()
{
memset(g,0x3f,sizeof g);
scanf("%d%d", &n, &m);
while (m -- ){
int a,b,c;
scanf("%d%d%d", &a, &b,&c);
g[a][b] = g[b][a]=min(g[a][b],c);
}
int t = prim();
if (t==INF) puts("impossible");
else
printf ("%d\n",t);
return 0;
}
边栏推荐
- Codeforces Round #798 (Div. 2)ABCD
- 求组合数四种方法
- vivo大规模 Kubernetes 集群自动化运维实践
- PyTorch基础(二)-- 张量与梯度
- Big O notation interpretation
- AcWing第 55 场周赛
- 2022煤矿探放水特种作业证考试题库模拟考试平台操作
- 宝塔中navicat连接mysql
- Necessary for Architects: system capacity status checklist
- 2022 coal mine water exploration and drainage special operation certificate examination question bank simulated examination platform operation
猜你喜欢

flutter简单优秀的开源dialog使用free_dialog

区间修改乘和加(理解懒标记的好例题)

Database learning notes (Chapter 16)

恶意代码实战分析Lab05-01

硬件工程师薪资虚高,你认可吗?

第七章 文件管理作业

Actual combat simulation │ real time error alarm of enterprise wechat robot

等个有“源”人|OpenHarmony 成长计划学生挑战赛报名启动
Some experience in database table structure design

Electrolytic capacitor, tantalum capacitor, ordinary capacitor
随机推荐
5.5 clock screensaver
Mysql database conceptual design using E-R model
SSM integration preliminary details
Brief request process
JGR-A | 南京大学黄安宁团队揭示高原湖泊山地影响极端降水的动力-热力机制
Codeforces Round #798 (Div. 2)ABCD
vivo大规模 Kubernetes 集群自动化运维实践
2020 ICPC Asia Taiwan Online Programming Contest C Circles
求组合数四种方法
flutter简单优秀的开源dialog使用free_dialog
D evaluate twice map
[dynamic planning] beginner level
硬件工程师薪资虚高,你认可吗?
宝塔添加一个网站:PHP项目
of_find_compatible_node查找出所有的节点
Go needs to add an arrow syntax, which is more like PHP!
How to optimize MySQL?
Introduction to recursive idea and implementation, eliminating recursion
Go zero microservice Practice Series (III. API definition and table structure design)
Vivo large scale kubernetes cluster automation operation and maintenance practice