本文出自: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+堆解法——代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define INF 0xffffff using namespace std; struct Edge
{
int v;
int w;
Edge(int v_, int w_):v(v_), w(w_){}
bool operator < (const Edge &e) const
{
return w > e.w;
}
}; typedef vector <Edge> vedge;
vector <vedge> g(110);
int n; int Prime(vector <vedge> & g)
{
int i, j, k;
vector <int> visit(n);
vector <int> dist(n);
priority_queue <Edge> pq;
for(i = 0; i < n; i++)
{
visit[i] = 0;
dist[i] = INF;
} Edge temp(0, 0); int nOwn = 0;
int nWeight = 0;
pq.push(Edge(0, 0)); while(nOwn < n && !pq.empty())
{
do
{
temp = pq.top();
pq.pop();
}
while(visit[temp.v] == 1 && !pq.empty());
if(visit[temp.v] == 0)
{
nOwn++;
nWeight += temp.w;
visit[temp.v] = 1;
}
for(i = 0 ; i < g[temp.v].size(); i++)
{
int v = g[temp.v][i].v;
if(visit[v] == 0)
{
int w = g[temp.v][i].w;
dist[v] = w;
pq.push(Edge(v, w));
}
}
}
if(nOwn < n)
{
cout << nOwn << n << endl;
return -1;
}
return nWeight;
} int main()
{
int i, j, k;
int temp;
while(~scanf("%d", &n))
{
for(i = 0; i < n; i++)
g[i].clear();
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
{
scanf("%d", &temp);
g[i].push_back(Edge(j, temp));
} cout << Prime(g) << endl;
}
return 0;
}

Kruscal算法代码:

//author: svtter
// #include <algorithm>
#include <vector>
#include <iostream>
#include <stdio.h>
#include <string.h> const int MAXN = 100000; using namespace std; vector <int> root;
int n;
struct Edge
{
int i, j, w;
Edge(int i_, int j_, int w_):i(i_), j(j_), w(w_){}
bool operator < (const Edge &e) const
{
return w < e.w;
}
}; void init()
{
for(int i = 0; i < n; i++)
root.push_back(i);
} int getRoot(int i)
{
if(root[i] == i)
return i;
return root[i] = getRoot(root[i]);
} void Merge(int i, int j)
{
int a, b;
a = getRoot(i);
b = getRoot(j);
if(a == b)
return;
//a's root is a;
root[a] = b;
} vector <Edge> g; int Kruskal()
{
//init the
init();
sort(g.begin(),g.end());
//the num of tree's Edges is n-1;
//
int nEdge = 0; //the weight of whole gtree
int nWeight = 0;
int i, s, e, w;
for(i = 0; i < g.size(); i++)
{
s = g[i].i;
e = g[i].j;
w = g[i].w;
if(getRoot(s) != getRoot(e))
{
Merge(s,e);
nWeight += w;
nEdge++;
}
if(nEdge == n-1)
break;
}
return nWeight;
} int main()
{
int i, j, k;
while(~scanf("%d", &n))
{
g.clear();
root.clear();
for(i = 0; i < n ; i++)
for(j = 0; j < n; j++)
{
scanf("%d", &k);
g.push_back(Edge(i, j, k));
} cout << Kruskal() << endl;
}
return 0;
}

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. JSON小结

    在 JSON 中,“Object” 是什么呢? json.org 有很好的解释: 1 .An object is an unordered set of name/value pairs. 2.An ...

  2. VB高清图标制作方法

    我隆重介绍一个软件:ResHacker !!! 这个软件可以修改软件的很多东西包括图标和标题,下面看**作. 运行ResHacker打开要更改图标的exe文件, 图标组--1--右键0--替换资源-- ...

  3. 使用jQuery和css3实现了仿淘宝ued博客左边的菜单切换动画

    今天看到淘宝ued博客的左侧导航菜单的动画好,要使用jQuery和css3我做一个简单的示例,主要用途是实现jQuery 事件和css3 transition属性. 个元素来实现鼠标滑动到某个导航的背 ...

  4. hdu 5030 Rabbit&#39;s String(后缀数组&amp;二分法)

    Rabbit's String Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. Linux在什么样的从脚本文件数据库sh格式改变sql格式

    在软件开发过程中,经常参与Linux从下一个脚本文件数据库sh格式改变sql格式问题.在本文中,一个实际的脚本文件,例如.描述格式转换过程.        1. sh文件内容 本文中的文件名称为exa ...

  6. ExtJs--02--MessageBox相关弹出窗口alert,prompt,confirm采用

    /* Ext.onReady(function(){ Ext.MessageBox.alert("jack","tom"); Ext.MessageBox.al ...

  7. freemarker该阵列

    freemarker该阵列 1.设计思路 (1)声明一个数组 (2)打印数组中的元素 2.设计源代码 <#--freemarker数组--> <#assign nums=[12,34 ...

  8. 于linux已安装moodle

    本文介绍了两个虚拟机的安装linux server 及相关服务,随着后这些基础.安装应用程序服务 moodle 2.7+  它是使用最广泛的平台,网络课程. 在安装过程中moodle之前,需要支持软件 ...

  9. T-SQL问题解决集锦——数据加解密

    原文:T-SQL问题解决集锦--数据加解密 以下代码已经在SQLServer2008上的示例数据库测试通过 问题一:如何为数据进行加密与解密,避免使用者窃取机密数据? 对于一些敏感数据,如密码.卡号, ...

  10. ANDROID嵌入式应用Unity3D视图(画廊3D模型)

    转载请注明来自大型玉米的博客文章(http://blog.csdn.net/a396901990),谢谢支持! 效果展示:   watermark/2/text/aHR0cDovL2Jsb2cuY3N ...