本文出自: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. 原来Github上的README.md文件这么有意思——Markdown语言详解

    转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 之前一直在使用github,也在上面分享了不少的项目和Demo,每次创建新项目的时候,使用的都是默认的REA ...

  2. 打印出所有&quot;水仙花数

    时间限制: 1 Sec  内存限制: 128 MB 提交: 695  解决: 352 [提交][状态][讨论版] 题目描述 打印出所有"水仙花数",所谓"水仙花数&quo ...

  3. Linux 在下面MATLAB下载

    网上找了很长一段时间都没有很好的连接,或者是一个普通的软件, 在这里,给大家一个连接: ed2k://|file|[%E7%9F%A9%E9%98%B5%E5%AE%9E%E9%AA%8C%E5%AE ...

  4. inner join on, left join on, right join on

    1.定义: inner join(等值连接) : 仅仅返回两个表中联结字段相等的记录 left join(左联接) :返回包含左表中的全部记录和右表中联结字段相等的记录 right join(右联接) ...

  5. Mybatis分页插件更新

    分页插件演示:http://blog.csdn.net/isea533/article/details/23831273 分页插件演示样例:http://blog.csdn.net/isea533/a ...

  6. poj3414--Pots(bfs,记录路径)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10149   Accepted: 4275   Special J ...

  7. 好大滴坑, Spring MVC覆盖了Trsaction

    好大滴坑. Spring MVC覆盖了Trsaction 解决方式: <!-- package-scan 4 Spring MVC --> <context:component-sc ...

  8. C++ 内部排序(一)

    先讲两个概念,所谓内部排序,指待排序的节点均存储在内存中.所谓排序的稳定性,指排序后,值相等的两个元素原来相对的位置是否发生变化了.举个例子. 待排序列:3(1),1,5,3(2)  稳定排序:1,3 ...

  9. Windows Phone获取WiFi BSSID

    原文:Windows Phone获取WiFi BSSID BSSID,一种特殊的Ad-hoc LAN的应用,也称为Basic Service Set (BSS),一群计算机设定相同的BSS名称,即可自 ...

  10. XeTeX中文解决方案(temp)

    临时记录一下XeTeX的中文解决方案.一些包的文档只是走马观花得到的解决方法,所以可能有诸多纰漏. 另个人还是比较看好LuaTeX,但是在里边鼓捣中文还是一团糟,等探索一下再回来补充. 我使用的包是x ...