prim求最小生成树
一直以来只会Kruskal
prim和dijkstra很像
只不过prim维护的是最短的边,而dijkstra维护的是最短的从起点到一个点的路径
同时prim要注意当前拓展的边是没有拓展过的
可以用堆优化
#include<bits/stdc++.h>
#define REP(i, a, b) for(register int i = (a); i < (b); i++)
#define _for(i, a, b) for(register int i = (a); i <= (b); i++)
using namespace std; const int MAXN = + ;
const int MAXM = 2e5 + ; struct Edge{ int to, w, next; };
Edge e[MAXM << ];
int head[MAXN], d[MAXN], vis[MAXN], n, m, tot; void AddEdge(int from, int to, int w)
{
e[tot] = Edge{to, w, head[from]};
head[from] = tot++;
} void read(int& x)
{
int f = ; x = ; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -; ch = getchar(); }
while(isdigit(ch)) { x = x * + ch - ''; ch = getchar(); }
x *= f;
} void prim()
{
vis[] = ;
_for(i, , n) d[i] = i == ? : 1e9;
for(int i = head[]; ~i; i = e[i].next)
{
int v = e[i].to;
d[v] = min(d[v], e[i].w);
} int ans = ;
REP(k, , n)
{
int mint = 1e9, id;
_for(i, , n)
if(!vis[i] && d[i] < mint)
{
mint = d[i];
id = i;
} ans += mint;
vis[id] = ; for(int i = head[id]; ~i; i = e[i].next)
{
int v = e[i].to;
if(vis[v]) continue;
d[v] = min(d[v], e[i].w);
}
}
printf("%d\n", ans);
} int main()
{
memset(head, -, sizeof(head)); tot = ;
read(n); read(m);
_for(i, , m)
{
int u, v, w;
read(u); read(v); read(w);
AddEdge(u, v, w);
AddEdge(v, u, w);
}
prim();
return ;
}
堆优化版本
#include<bits/stdc++.h>
#define REP(i, a, b) for(register int i = (a); i < (b); i++)
#define _for(i, a, b) for(register int i = (a); i <= (b); i++)
using namespace std; const int MAXN = + ;
const int MAXM = 2e5 + ; struct Edge{ int to, w, next; };
Edge e[MAXM << ];
int head[MAXN], d[MAXN], vis[MAXN], n, m, tot; void AddEdge(int from, int to, int w)
{
e[tot] = Edge{to, w, head[from]};
head[from] = tot++;
} void read(int& x)
{
int f = ; x = ; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -; ch = getchar(); }
while(isdigit(ch)) { x = x * + ch - ''; ch = getchar(); }
x *= f;
} struct node
{
int id, w;
bool operator < (const node& rhs) const
{
return w > rhs.w;
}
};
priority_queue<node> q; void prim()
{
vis[] = ;
_for(i, , n) d[i] = i == ? : 1e9;
for(int i = head[]; ~i; i = e[i].next)
{
int v = e[i].to;
d[v] = min(d[v], e[i].w);
}
_for(i, , n) q.push(node{i, d[i]}); int ans = ;
REP(k, , n)
{
int mint, id;
while()
{
node x = q.top(); q.pop();
if(vis[x.id]) continue;
id = x.id, mint = x.w;
break;
} ans += mint;
vis[id] = ; for(int i = head[id]; ~i; i = e[i].next)
{
int v = e[i].to;
if(vis[v]) continue;
if(d[v] > e[i].w)
{
d[v] = e[i].w;
q.push(node{v, d[v]});
}
}
}
printf("%d\n", ans);
} int main()
{
memset(head, -, sizeof(head)); tot = ;
read(n); read(m);
_for(i, , m)
{
int u, v, w;
read(u); read(v); read(w);
AddEdge(u, v, w);
AddEdge(v, u, w);
}
prim();
return ;
}
prim求最小生成树的更多相关文章
- Codeforces 632F - Magic Matrix(暴力 bitset or Prim 求最小生成树+最小瓶颈路)
题面传送门 开始挖老祖宗(ycx)留下来的东西.jpg 本来想水一道紫题作为 AC 的第 500 道紫题的,结果发现点开了道神题. 首先先讲一个我想出来的暴力做法.条件一和条件二直接扫一遍判断掉.先将 ...
- POJ 1258 Agri-Net(Prim求最小生成树)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64912 Accepted: 26854 Descri ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- 新疆大学(新大)OJ xju 1009: 一带一路 prim求最短路径+O(n)素数筛选
1009: 一带一路 时间限制: 1 Sec 内存限制: 128 MB 题目描述 一带一路是去去年习大大提出来的建设“新丝绸之路经济带”和“21世纪海上丝绸之路”的战略构想.其中就包括我们新疆乌鲁木 ...
- HDU-1233 还是畅通工程 (prim 算法求最小生成树)
prim 算法求最小生成树 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- Kruskal和Prim算法求最小生成树
Kruskal算法求最小生成树 测试数据: 5 6 0 1 5 0 2 3 1 2 4 2 4 2 2 3 1 1 4 1 输出: 2 3 1 1 4 1 2 4 2 0 2 3 思路:在保证不产生回 ...
- Prim算法和Kruskal算法求最小生成树
Prim算法 连通分量是指图的一个子图,子图中任意两个顶点之间都是可达的.最小生成树是连通图的一个连通分量,且所有边的权值和最小. 最小生成树中,一个顶点最多与两个顶点邻接:若连通图有n个顶点,则最小 ...
- 求最小生成树(暴力法,prim,prim的堆优化,kruskal)
求最小生成树(暴力法,prim,prim的堆优化,kruskal) 5 71 2 22 5 21 3 41 4 73 4 12 3 13 5 6 我们采用的是dfs的回溯暴力,所以对于如下图,只能搜索 ...
- 858. Prim算法求最小生成树(模板)
给定一个n个点m条边的无向图,图中可能存在重边和自环,边权可能为负数. 求最小生成树的树边权重之和,如果最小生成树不存在则输出impossible. 给定一张边带权的无向图G=(V, E),其中V表示 ...
随机推荐
- 【cl】cmd相关命令
cd 进入目录 dir 列出当前目录下的文件[在linux上是ls] e: 进入E盘 tab键可以快速进入目录
- android 更新ui
https://www.cnblogs.com/rayray/p/3437048.html https://www.cnblogs.com/zhaoyanjun/p/5546683.html
- Android+Jquery Mobile学习系列-目录
最近在研究学习基于Android的移动应用开发,准备给家里人做一个应用程序用用.向公司手机移动团队咨询了下,觉得使用Android的WebView上手最快,因为WebView等于是一个内置浏览器,可以 ...
- Node.js:template
ylbtech-Node.js: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech ...
- shp系列(六)——利用C++进行Dbf文件的写(创建)
上一篇介绍了shp文件的创建,接下来介绍dbf的创建. 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 1.Dbf头文件的创建 Dbf头文件的结构 ...
- BZOJ 4032 trie树+各种乱搞
思路 : 先对b 的所有后缀建立trie树 第一问 暴力枚举a串的起点 在trie树上跑 找到最短的 第二问 也是暴力枚举a串的起点 a和b顺着暴力匹配就好 第三问 求出来a在第i个位置 加一个字母j ...
- 初学 Ajax(涉及 php)
一直知道 ajax 但是尚未真正了解, 这次看了慕课网的<Ajax全接触>,算是有所收获,入了个门. 需要用到php,因为 Ajax也是向服务器请求(不知道这么解释对不对), 所以还需要配 ...
- 第六课: - GroupBy函数
第 6 课 让我们看一看 groupby 函数. In [1]: # Import libraries import pandas as pd import sys In [2]: print(' ...
- JavaScript实现数字时钟功能
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...
- angular实现的tab栏切换
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...