一直以来只会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求最小生成树的更多相关文章

  1. Codeforces 632F - Magic Matrix(暴力 bitset or Prim 求最小生成树+最小瓶颈路)

    题面传送门 开始挖老祖宗(ycx)留下来的东西.jpg 本来想水一道紫题作为 AC 的第 500 道紫题的,结果发现点开了道神题. 首先先讲一个我想出来的暴力做法.条件一和条件二直接扫一遍判断掉.先将 ...

  2. POJ 1258 Agri-Net(Prim求最小生成树)

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64912   Accepted: 26854 Descri ...

  3. HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

    这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...

  4. 新疆大学(新大)OJ xju 1009: 一带一路 prim求最短路径+O(n)素数筛选

    1009: 一带一路 时间限制: 1 Sec  内存限制: 128 MB 题目描述 一带一路是去去年习大大提出来的建设“新丝绸之路经济带”和“21世纪海上丝绸之路”的战略构想.其中就包括我们新疆乌鲁木 ...

  5. HDU-1233 还是畅通工程 (prim 算法求最小生成树)

    prim 算法求最小生成树 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  6. 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 思路:在保证不产生回 ...

  7. Prim算法和Kruskal算法求最小生成树

    Prim算法 连通分量是指图的一个子图,子图中任意两个顶点之间都是可达的.最小生成树是连通图的一个连通分量,且所有边的权值和最小. 最小生成树中,一个顶点最多与两个顶点邻接:若连通图有n个顶点,则最小 ...

  8. 求最小生成树(暴力法,prim,prim的堆优化,kruskal)

    求最小生成树(暴力法,prim,prim的堆优化,kruskal) 5 71 2 22 5 21 3 41 4 73 4 12 3 13 5 6 我们采用的是dfs的回溯暴力,所以对于如下图,只能搜索 ...

  9. 858. Prim算法求最小生成树(模板)

    给定一个n个点m条边的无向图,图中可能存在重边和自环,边权可能为负数. 求最小生成树的树边权重之和,如果最小生成树不存在则输出impossible. 给定一张边带权的无向图G=(V, E),其中V表示 ...

随机推荐

  1. C++ exit 与 return 浅析

    [摘要] 本文从代码形式.经常使用方式,相关概念,调用关系和比較分析,这5个维度浅析 exit 与 return 在C++的同样点与差别. [常见形式] exit(0):   正常执行程序并退出程序. ...

  2. 《从零開始学Swift》学习笔记(Day48)——类型检查与转换

    原创文章,欢迎转载.转载请注明:关东升的博客 继承会发生在子类和父类之间,是一系列类的继承关系. 比如:Person是类层次结构中的根类.Student是Person的直接子类.Worker是Pers ...

  3. GammaRay 是一个允许你查看 Qt 应用程序甚至在某种程度上修改它的独特应用,可谓是 Debugger 的良好补充

    GammaRay is a tool to poke around in a Qt-application and also to manipulate the application to some ...

  4. oc19--继承1

    // // Phone.h // day13 #import <Foundation/Foundation.h> // 被继承的这个类我们称之为父类/ 超类 @interface Phon ...

  5. poj3621 Sightseeing Cows

    01分数规划 二分+spfa负环(SLF优化) #include<cstdio> #include<iostream> #include<cstring> #inc ...

  6. 国内物联网平台初探(五) ——机智云IoT物联网云服务平台及智能硬件自助开发平台

    平台定位 机智云平台是致力于物联网.智能硬件云服务的开放平台.平台提供了从定义产品.设备端开发调试.应用开发.产测.运营管理等覆盖智能硬件接入到运营管理全生命周期服务的能力. 机智云平台为开发者提供了 ...

  7. php中全局变量global和超全局变量$GLOBALS

    php中全局变量global和超全局变量$GLOBALS 1.global Global的作用是定义全局变量,但是这个全局变量不是应用于整个网站,而是应用于当前页面,包括include或require ...

  8. tflearn Training Step每次 We will run it for 10 epochs (the network will see all data 10 times) with a batch size of 16. n_epoch=10, batch_size=16

    Training TFLearn provides a model wrapper 'DNN' that can automatically performs a neural network cla ...

  9. [Plugin] WEB版一次选择多个文件进行批量上传(swfupload)的解决方案

    URL:http://www.cnblogs.com/chillsrc/archive/2010/02/21/1670594.html 说明:功能完全支持ie和firefox浏览器! 一般的WEB方式 ...

  10. openssl https证书

    今天摸索了下 HTTPS 的证书生成,以及它在 Nginx 上的部署.由于博客托管在 github 上,没办法部署证书,先记录下,后续有需要方便快捷操作.本文的阐述不一定完善,但是可以让一个初学者了解 ...