本文出自:http://blog.csdn.net/svitter

题意:给出一个数字n代表邻接矩阵的大小,随后给出邻接矩阵的值。输出最小生成树的权值。

题解:

prime算法的基本解法;

1.选择一个点,然后不停的向当中增加权值最小的边,边的一端在已经生成的部分生成树中,还有一端在未生成的生成树中。

2.利用优先队列维护边,将增加的点所包括的边增加到队列中去,随后依照边的权值弹出。

简单理解方法:一个人能够拉非常多人,新被拉进来的人,把能拉的人(有边,且未被訪问)排队,找最好拉的人拉进来,循环。

注意:

1.假设使用priority_queue(二叉堆)+prime算法,时间复杂度为ElogV

2.直接使用邻接矩阵,复杂度为O(n^2)

3.使用STL 优先队列的时候记得定义排序方法;(见代码:14行)

4.记得清空vector数组

Kruskal算法的基本解法:

1.Kruskal算法存的是边。将全部边存起来,然后依照从小到大排序,依次增加两端不再同一集合的边。

2.复杂度为O(ElogE)

3.稀疏图

简单理解方法:几个人抱团,最后在全都拉在一起。

树的特点:边的个数是n-1,所以增加n-1条边就可以停止。

Prime+堆解法——代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <queue>
  5. #define INF 0xffffff
  6.  
  7. using namespace std;
  8.  
  9. struct Edge
  10. {
  11. int v;
  12. int w;
  13. Edge(int v_, int w_):v(v_), w(w_){}
  14. bool operator < (const Edge &e) const
  15. {
  16. return w > e.w;
  17. }
  18. };
  19.  
  20. typedef vector <Edge> vedge;
  21. vector <vedge> g(110);
  22. int n;
  23.  
  24. int Prime(vector <vedge> & g)
  25. {
  26. int i, j, k;
  27. vector <int> visit(n);
  28. vector <int> dist(n);
  29. priority_queue <Edge> pq;
  30. for(i = 0; i < n; i++)
  31. {
  32. visit[i] = 0;
  33. dist[i] = INF;
  34. }
  35.  
  36. Edge temp(0, 0);
  37.  
  38. int nOwn = 0;
  39. int nWeight = 0;
  40. pq.push(Edge(0, 0));
  41.  
  42. while(nOwn < n && !pq.empty())
  43. {
  44. do
  45. {
  46. temp = pq.top();
  47. pq.pop();
  48. }
  49. while(visit[temp.v] == 1 && !pq.empty());
  50. if(visit[temp.v] == 0)
  51. {
  52. nOwn++;
  53. nWeight += temp.w;
  54. visit[temp.v] = 1;
  55. }
  56. for(i = 0 ; i < g[temp.v].size(); i++)
  57. {
  58. int v = g[temp.v][i].v;
  59. if(visit[v] == 0)
  60. {
  61. int w = g[temp.v][i].w;
  62. dist[v] = w;
  63. pq.push(Edge(v, w));
  64. }
  65. }
  66. }
  67. if(nOwn < n)
  68. {
  69. cout << nOwn << n << endl;
  70. return -1;
  71. }
  72. return nWeight;
  73. }
  74.  
  75. int main()
  76. {
  77. int i, j, k;
  78. int temp;
  79. while(~scanf("%d", &n))
  80. {
  81. for(i = 0; i < n; i++)
  82. g[i].clear();
  83. for(i = 0; i < n; i++)
  84. for(j = 0; j < n; j++)
  85. {
  86. scanf("%d", &temp);
  87. g[i].push_back(Edge(j, temp));
  88. }
  89.  
  90. cout << Prime(g) << endl;
  91. }
  92. return 0;
  93. }

Kruscal算法代码:

  1. //author: svtter
  2. //
  3.  
  4. #include <algorithm>
  5. #include <vector>
  6. #include <iostream>
  7. #include <stdio.h>
  8. #include <string.h>
  9.  
  10. const int MAXN = 100000;
  11.  
  12. using namespace std;
  13.  
  14. vector <int> root;
  15. int n;
  16. struct Edge
  17. {
  18. int i, j, w;
  19. Edge(int i_, int j_, int w_):i(i_), j(j_), w(w_){}
  20. bool operator < (const Edge &e) const
  21. {
  22. return w < e.w;
  23. }
  24. };
  25.  
  26. void init()
  27. {
  28. for(int i = 0; i < n; i++)
  29. root.push_back(i);
  30. }
  31.  
  32. int getRoot(int i)
  33. {
  34. if(root[i] == i)
  35. return i;
  36. return root[i] = getRoot(root[i]);
  37. }
  38.  
  39. void Merge(int i, int j)
  40. {
  41. int a, b;
  42. a = getRoot(i);
  43. b = getRoot(j);
  44. if(a == b)
  45. return;
  46. //a's root is a;
  47. root[a] = b;
  48. }
  49.  
  50. vector <Edge> g;
  51.  
  52. int Kruskal()
  53. {
  54. //init the
  55. init();
  56. sort(g.begin(),g.end());
  57. //the num of tree's Edges is n-1;
  58. //
  59. int nEdge = 0;
  60.  
  61. //the weight of whole gtree
  62. int nWeight = 0;
  63. int i, s, e, w;
  64. for(i = 0; i < g.size(); i++)
  65. {
  66. s = g[i].i;
  67. e = g[i].j;
  68. w = g[i].w;
  69. if(getRoot(s) != getRoot(e))
  70. {
  71. Merge(s,e);
  72. nWeight += w;
  73. nEdge++;
  74. }
  75. if(nEdge == n-1)
  76. break;
  77. }
  78. return nWeight;
  79. }
  80.  
  81. int main()
  82. {
  83. int i, j, k;
  84. while(~scanf("%d", &n))
  85. {
  86. g.clear();
  87. root.clear();
  88. for(i = 0; i < n ; i++)
  89. for(j = 0; j < n; j++)
  90. {
  91. scanf("%d", &k);
  92. g.push_back(Edge(i, j, k));
  93. }
  94.  
  95. cout << Kruskal() << endl;
  96. }
  97. return 0;
  98. }

POJ1258 基础最小生成树的更多相关文章

  1. POJ1258 (最小生成树prim)

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

  2. hdu1102 Constructing Roads 基础最小生成树

    //克鲁斯卡尔(最小生成树) #include<cstdio> #include<iostream> #include<algorithm> using names ...

  3. poj1258 Agri-Net 最小生成树

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

  4. hdu1162 Eddy's picture 基础最小生成树

    #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...

  5. hdu1301 Jungle Roads 基础最小生成树

    #include<iostream> #include<algorithm> using namespace std; ; int n, m; ]; struct node { ...

  6. hdu1879 继续畅通工程 基础最小生成树

    #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> u ...

  7. hdu1875 畅通工程再续 暴力+基础最小生成树

    #include<cstdio> #include<cmath> #include<algorithm> using namespace std; ; ; ; in ...

  8. hdu1863 畅通工程 基础最小生成树

    #include <iostream> #include <cstdio> #include <algorithm> #define N 110 #define M ...

  9. hdu1233 还是畅通工程 基础最小生成树

    //克鲁斯卡尔 #include<iostream> #include<algorithm> using namespace std; ; struct node { int ...

随机推荐

  1. git bash 出现vim的时候怎么退出

    如果是输出状态,首先Esc退出输入状态,然后Shift+;,再输入q!或wq!(不保存改动,wq!是保存文件的写入修改)退出

  2. Microsoft Toolkit 2.5下载 – 一键激活Windows 8.1/2012 R2/Office 2013

    http://www.dayanzai.me/microsoft-toolkit-2-5.html

  3. AsyncSocket长连接棒包装问题解决

    project正在使用长连接快来server沟通.因此,指定我们的协议前两个字节为数据长度来区分数据包 app这边数据有两种传输形式: 1.app主动请求所须要的数据: 2.app异步接收来自服务端的 ...

  4. DuiVision开发教程(15)-DUI文本控制基础类

    CControlBaseFont类是DuiVision支持所有基类的控件的文本属性. 此控件例如属性列表,下面: 物业名称 类型 说明 title 字符串 控件的显示标题 font 字体 控件的字体, ...

  5. JAVA设计模式--辛格尔顿

    Singleton模式可以作为一种编程技术,让我们先从理论上说代码 单例模式三个关键点: 1).某个类仅仅能有一个实例 2).该类必须自行创建这个实例 3).该类必须自行向整个系统提供这个实例 应用场 ...

  6. codeigniter 该脚本在运行300s超时退

    直接看代码, file:system/core/CodeIgniter.php /* 102  * -------------------------------------------------- ...

  7. Quartz.net开源作业调度

    Quartz.net开源作业调度框架使用详解 前言 quartz.net作业调度框架是伟大组织OpenSymphony开发的quartz scheduler项目的.net延伸移植版本.支持 cron- ...

  8. redhat6.3已安装was6.1你可以不弹出安装程序

    这在为期两天的课程redhat6.3安装was6.1 使用Xmanager打开图形界面.进入/WAS夹,跑./install 它有一个直接跳转,不管是什么反应,起初我以为这个问题的图形界面,搜索了半天 ...

  9. mysql 解压缩和赋权

    拉开拉链mysql紧凑根文件夹 注意ini配置文件的内容 basedir = D:\mysql-5.6.17-winx64  datadir = D:\mysql-5.6.17-winx64  por ...

  10. JAVA —— 数组

    import java.util.Arrays; public class Array { public static void main(String[] args){    Array test= ...