prim算法——小根堆实现
来源:互联网 发布:手机淘宝网登录在哪里 编辑:程序博客网 时间:2024/06/06 12:38
#include <iostream>#include <queue>#include <vector>using namespace std;typedef pair<int,int> p;const int max_n=1000;const int inf=(1<<30);vector<p> m[max_n];bool used[max_n];int v,e;int prim(){priority_queue<p,vector<p>,greater<p> > q;q.push(p(0,1));int res=0,num=0;while(!q.empty()){p t=q.top();q.pop();num++;res+=t.first;int v=t.second;used[v]=1;for(int i=0;i<m[v].size();i++){p temp=m[v][i];if(!used[temp.second]) q.push(temp); }while(!q.empty()&&used[q.top().second]) q.pop();}if(num==v) return res;else return -1;// 表示是一个非连通图 }int main(){int from,to,c;while(cin>>v>>e,~v){for(int i=0;i<v+1;i++){m[i].clear();used[i]=false;}while(e--){cin>>from>>to>>c;m[from].push_back(p(c,to));m[to].push_back(p(c,from));}cout<<prim()<<endl;}return 0;}/*测试范例:7 9 //结点数和边数1 2 12 3 22 4 33 5 102 7 74 7 14 6 55 7 56 7 8*/
阅读全文
0 0
- prim算法——小根堆实现
- POJ3625—Prim实现
- 最小生成树——prim算法实现
- C语言——Prim算法实现最小生成树
- Prim算法——poj1258
- 图——Prim算法
- Prim算法实现
- Prim算法实现类
- Prim算法实现
- Prim算法的实现
- c#实现Prim算法
- Prim算法的实现
- Python 实现 Prim算法
- prim 算法实现
- matlab实现prim算法
- prim算法实现
- 邻接矩阵实现prim算法
- java实现Prim算法
- IDL之指针
- Java_基础—集合嵌套之ArrayList嵌套ArrayList
- android 数据持久化技术
- shell中的特殊变量及用法
- 面向对象-继承
- prim算法——小根堆实现
- WebService集成Spring
- 从零开始玩转JMX(一)——简介和Standard MBean
- 剑指offer--面试题18:删除链表的结点
- bzoj 2281: [Sdoi2011]黑白棋 博弈论+动态规划+排列组合
- Linux下的shell进度条
- 一些资源以及读书总结
- mysql中类型长度以及text和varchar的区别
- Linux基础(2)