prim模板
- 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模板的更多相关文章
- HDU1875+Prim模板
https://cn.vjudge.net/problem/HDU-1875 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府 ...
- HDU 1223 还是畅通工程(最小生成树prim模板)
一个很简单的prim模板,但虽然是模板,但也是最基础的,也要脱离模板熟练打出来 后期会更新kruskal写法 #include<iostream> #include<cstdio&g ...
- Kruskal && Prim模板
1. Kruskal(并查集模板): /* Kruskal:并查集实现,记录两点和距离,按距离升序排序,O (ElogE) */ struct Edge { int u, v, w; bool ope ...
- prim模板题
题目链接:http://acm.hrbeu.edu.cn/index.php?act=problem&id=1223 #include <cstdio> #include < ...
- 最小生成树(kruskal模版 Prim模板)
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2144&cid=1186 最小生成树,最重要的是了解思想 稠密图用Prim,稀疏图用Kru ...
- poj 1258 Agri-Net prim模板 prim与dijkstra的区别
很裸地求最小生成树的题目.题意就不多说了,最重要的就是记录一下学会了prim算法. 初学prim,给我的第一感觉就是和dijkstra好像啊,感觉两者的区别还是有的: 1:prim是求最小生成树的算法 ...
- prim 模板
#include<cstdio> #include<vector> #include<cstring> #include<set> #define ma ...
- 最小生成树模板【kruskal & prim】
CDOJ 1966 Kruskal 解法 时间复杂度O(mlogm) m为边数,这里主要是边排序占时间,后面并查集还好 #include <cstdio> #include <cst ...
- 最小生成树(次小生成树)(最小生成树不唯一) 模板:Kruskal算法和 Prim算法
Kruskal模板:按照边权排序,开始从最小边生成树 #include<algorithm> #include<stdio.h> #include<string.h> ...
随机推荐
- 如何得到Sessionid的值
当用户向一个网站请求第一个页面时,用户会话启动.当第一个页面被请求时,web服务器将asp.net_sessionID cookie添加进用户的浏览器.可以使用newsession属性探测新会话的启 ...
- video标签 拖动 转自w3school
调整视频大小 播放 暂停 用js实现 详细参见http://www.w3school.com.cn/tiy/t.asp?f=html5_video_dom 图片的拖动详见http://www.w3sc ...
- asp.net发布网站(转)
1. 在Web项目中点击发布网站,如图1所示 图1 2. 选择要发布的路径——>“确定”,如果项目显示发布成功就可以了.如图2所示 图2 3. 打 ...
- SqlServer 2015修改表时出现“save changes is not permitted…”的解决方法
使用SqlServer 2015的过程中,会出现如下情况: 在修改完表字段名或是类型后点击保存时会弹出一个对话框,且无法保存已做的修改.对话框内容大致如下: Saving changes is not ...
- [NewCoder]复杂链表的复制
看下面一个链表结点的定义: struct ComplexListNode { int val; struct ComplexListNode *next; struct ComplexListNode ...
- Servlet url-pattern优先级
完全匹配>目录匹配>扩展名匹配
- 转几篇关于Android webView的网文
1,控件WebView显示网页 http://www.cnblogs.com/tinyphp/p/3858997.html http://blog.csdn.net/t12x3456/article/ ...
- linq to entity asp.net mvc 多字段排序
字段1 降序 字段2 降序 var str = db.xxx.OrderByDescending(p=>p.字段1).ThenByDescending(p=>p.字段2) ThenBy - ...
- Qt多线程编程总结(二)——QMutex
QMutex类提供的是线程之间的访问顺序化. QMutex的目的是保护一个对象.数据结构或者代码段,所以同一时间只有一个线程可以访问它.(在Java术语中,它和同步关键字“synchronized”很 ...
- c++ cout 保留小数点位
需要头文件 <iomanip> 输出时需要用 fixed 和 setprecision() fixed代表输出浮点数,setprecision()设置精度. #include <io ...