int vis[];
int n;
int map[][];
int d[];
int prim(){
int i,j,mi,v;
for(i=;i<n;i++){
d[i]=map[][i];
vis[i]=;
}
for(i=;i<=n;i++){
mi=<<;
for(j=;j<n;j++){
if(!vis[j] && mi>d[j]){
v=j;
mi=d[j];
}
}
vis[v]=;
for(j=;j<n;j++)
if(!vis[j] && d[j]>map[v][j])
d[j]=map[v][j];
}
for(i=;i<n;i++) d[]+=d[i];
return d[];
}

heap优化

http://www.nocow.cn/index.php/Prim%E7%AE%97%E6%B3%95

 /*
二叉堆优化Prim算法
Author:YangZX
Date:9.11 2011
*/
#include <iostream>
using namespace std;
const int MAXV = , MAXE = , INF = (~0u)>>;
struct edge{
int t, w, next;
}es[MAXE * ];
int h[MAXV], cnt, n, m, heap[MAXV], size, pos[MAXV], dist[MAXV];
void addedge(int x, int y, int z)
{
es[++cnt].t = y;
es[cnt].next = h[x];
es[cnt].w = z;
h[x] = cnt;
} void heapup(int k)
{
while(k > ){
if(dist[heap[k>>]] > dist[heap[k]]){
swap(pos[heap[k>>]], pos[heap[k]]);
swap(heap[k>>], heap[k]);
k>>=;
}else
break;
}
}
void heapdown(int k)
{
while((k<<) <= size){
int j;
if((k<<) == size || dist[heap[(k<<)]] < dist[heap[(k<<)+]])
j = (k<<);
else
j = (k<<) + ;
if(dist[heap[k]] > dist[heap[j]]){
swap(pos[heap[k]], pos[heap[j]]);
swap(heap[k], heap[j]);
k=j;
}else
break;
}
}
void push(int v, int d)
{
dist[v] = d;
heap[++size] = v;
pos[v] = size;
heapup(size);
}
int pop()
{
int ret = heap[];
swap(pos[heap[size]], pos[heap[]]);
swap(heap[size], heap[]);
size--;
heapdown();
return ret;
} int prim()
{
int mst = , i, p;
push(, );
for(i=; i<=n; i++)
push(i, INF);
for(i=; i<=n; i++){
int t = pop();
mst += dist[t];
pos[t] = -;
for(p = h[t]; p; p = es[p].next){
int dst = es[p].t;
if(pos[dst] != - && dist[dst] > es[p].w){
dist[dst] = es[p].w;
heapup(pos[dst]);
heapdown(pos[dst]);
}
}
}
return mst;
}
int main()
{
cin>>n>>m;
for(int i=; i<=m; i++){
int x, y, z;
cin>>x>>y>>z;
addedge(x, y, z);
addedge(y, x, z);
}
cout<<prim()<<endl;
return ;
}

prim模板的更多相关文章

  1. HDU1875+Prim模板

    https://cn.vjudge.net/problem/HDU-1875 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府 ...

  2. HDU 1223 还是畅通工程(最小生成树prim模板)

    一个很简单的prim模板,但虽然是模板,但也是最基础的,也要脱离模板熟练打出来 后期会更新kruskal写法 #include<iostream> #include<cstdio&g ...

  3. Kruskal && Prim模板

    1. Kruskal(并查集模板): /* Kruskal:并查集实现,记录两点和距离,按距离升序排序,O (ElogE) */ struct Edge { int u, v, w; bool ope ...

  4. prim模板题

    题目链接:http://acm.hrbeu.edu.cn/index.php?act=problem&id=1223 #include <cstdio> #include < ...

  5. 最小生成树(kruskal模版 Prim模板)

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2144&cid=1186 最小生成树,最重要的是了解思想 稠密图用Prim,稀疏图用Kru ...

  6. poj 1258 Agri-Net prim模板 prim与dijkstra的区别

    很裸地求最小生成树的题目.题意就不多说了,最重要的就是记录一下学会了prim算法. 初学prim,给我的第一感觉就是和dijkstra好像啊,感觉两者的区别还是有的: 1:prim是求最小生成树的算法 ...

  7. prim 模板

    #include<cstdio> #include<vector> #include<cstring> #include<set> #define ma ...

  8. 最小生成树模板【kruskal & prim】

    CDOJ 1966 Kruskal 解法 时间复杂度O(mlogm) m为边数,这里主要是边排序占时间,后面并查集还好 #include <cstdio> #include <cst ...

  9. 最小生成树(次小生成树)(最小生成树不唯一) 模板:Kruskal算法和 Prim算法

    Kruskal模板:按照边权排序,开始从最小边生成树 #include<algorithm> #include<stdio.h> #include<string.h> ...

随机推荐

  1. ubuntu之iptables

    1.更新iptables并立即生效: a.保存当前设置:iptables-save > /etc/iptables.up.rules b.修改iptables规则: 例如: -I INPUT - ...

  2. std::map中函数用法集合

    1 STL的map表里有一个erase方法用来从一个map中删除掉指令的节点  2 eg:  3 map<string,string> mapTest;  4 typedef map< ...

  3. MVC过滤器基本使用

        Action过滤器 /// <summary> /// 执行代码前执行 /// </summary> /// <param name="filterCo ...

  4. mysq数据库管理工具navicat基本使用方法

    navicat是mysql数据库的客户端查询管理工具,本文详细的介绍了该软件的基本使用方法 本文转自 http://hejiawangjava.iteye.com/blog/2245758 sql是操 ...

  5. 数组有没有 length()这个方法? String 有没有 length()这 个方法?

    1.数组中有length属性. 2.String有lenth()方法.

  6. GBK转utf-8,宽字符转窄字符

    //GBK转UTF8 string CAppString::GBKToUTF8(const string & strGBK) { string strOutUTF8 = "" ...

  7. [Swust OJ 581]--彩色的石子(状压dp)

    题目链接:http://acm.swust.edu.cn/problem/0581/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  8. Lucence.net索引技术 一

    1.建立索引 为了对文档进行索引,Lucene 提供了五个基础的类,他们分别是 Document, Field, IndexWriter, Analyzer, Directory.下面我们分别介绍一下 ...

  9. CDH 无法查看history log

    1.配置(core-site.xml) <property>  <name>hadoop.http.staticuser.user</name>  <valu ...

  10. HTML+CSS笔记 CSS入门

    简介: </span>年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的<span>脚本解释程序</span>,作为ABC语言的一种继承. & ...