洛谷P1546 最短网络 Agri-Net(Prim堆优化)
2024-09-06 12:36:27
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int INF=0x3f3f3f3f;
inline void read(int &tmp)
{
int x=;char c=getchar();
for(tmp=;!isdigit(c);c=getchar()) if(c=='-') x=-;
for(;isdigit(c);tmp=tmp*+c-,c=getchar());
tmp*=x;
}
int n,sum,tot,m,mp[maxn][maxn],w[maxn];
typedef pair<int,int> P;
priority_queue< P,vector<P>,greater<P> >q;
bool v[maxn];
void Prim()
{
memset(w,0x3f,sizeof(w));
w[]=;q.push(make_pair(,));
while(!q.empty())
{
int Top=q.top().second;q.pop();
if(v[Top]) continue;
v[Top]=;sum+=w[Top];++tot;
for(int i=;i<=n;i++)
if(mp[Top][i]<w[i]&&mp[Top][i]>&&v[i]==false) {w[i]=mp[Top][i];q.push(make_pair(w[i],i));}
}
}
int main()
{
read(n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{read(mp[i][j]);if(i==j) mp[i][j]=INF;}
Prim();
printf("%d",sum);
return ;
}
最新文章
- Windows Phone 的 TextBox 的实现 PropertyChanged
- UIKit - scrollView缩放、滚动
- VS2008 工程中部分文件不参与编译 从生成中排除【Worldsing笔记】
- KAFKA分布式消息系统
- 从CR线下活动学到的:如何组织一个小的线下活动
- SQL Server阻止了对组件xp_cmdshell过程的解决方案
- android studio c++ 自动补全
- 处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC"
- Learning React Native笔记
- Lucene教程 -------(一、初始Lucene)
- Python Selenium之异常处理
- Python函数默认参数的陷阱
- java中线程的三种实现方式
- HDU 1026(迷宫 BFS+打印)
- Miller Rabin素数检测
- P2731 骑马修栅栏 欧拉函数
- 第三部分:Android 应用程序接口指南---第二节:UI---第十一章 样式和主题
- Xcode连接TFS Git用户名和密码不正确解决方案
- Lua和C++交互 学习记录之五:全局数组交互
- PyCharm中的Console自动换行
热门文章
- 使用yarn代替npm
- C# WPF 仿QQ靠近屏幕上方自动缩起功能实现
- 超高频率问题之信息: Illegal access: this web application instance has been stopped already. Could not load . The eventual following stack trace is caused by an error thrown for debugging purposes as well as
- 在Vmware安装虚拟机WindowsServer 2003
- Django 的学习(1) 从建立到数据库操作
- Leetcode74. Search a 2D Matrix搜索二维矩阵
- sklearn之特征提取(文本特征)
- YouTube上最受欢迎的十大机器学习视频(最新)
- [Vue CLI 3] 插件开发之 registerCommand 到底做了什么
- 计算机组成原理作业一 熟悉MIPS指令