本文出自: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. SNMP WINDOWS系统的命令行工具下载

    SNMP windows系统的命令行工具snmputil.exe下载链接:请点击

  2. ASP.NET分页正品—分页真

     承接上篇博文<ASP.NET真假分页-假分页>:http://blog.csdn.net/u010773667/article/details/38845009,继续解说ASP.NE ...

  3. asp.net弹出层实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.c ...

  4. 第七个问题(枚举和set)

    set添加元素是基于equals和hashCode函数来确定的两个要素是否是同一物体. public final boolean equals(Object other) 当指定对象等于此枚举常量时, ...

  5. UVA 1358 - Generator(dp+高斯消元+KMP)

    UVA 1358 - Generator option=com_onlinejudge&Itemid=8&page=show_problem&category=524& ...

  6. NBA球员 ESPN前20排名:詹姆斯再居榜首

    2014年 ESPN球员实力最后排名 没想到 保罗-乔治没有上榜.之前预測的火箭两大明星排名落后了,杜兰特排到第8有点出乎意料. 戴维斯升得那么快有点出乎意料,一个球队拥有两位排前十位的球队:骑士,快 ...

  7. 【Flume】flume于transactionCapacity和batchSize进行详细的分析和质疑的概念

    我不知道你用flume读者熟悉无论这两个概念 一开始我是有点困惑,? 没感觉到transactionCapacity的作用啊? batchSize又是干啥的啊? -- -- 带着这些问题,我们深入源代 ...

  8. LinkedBlockingQueue的put,add跟offer的区别(转)

    LinkedBlockingQueue的put,add和offer的区别 最近在学习<<Java并发编程实践>>,有很多java.util.concurrent包下的新类.Li ...

  9. Android 异步消息处理机制 让你在深入了解 Looper、Handler、Message之间的关系

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/38377229 ,本文出自[张鸿洋的博客] 非常多人面试肯定都被问到过,请问And ...

  10. whu oj 1551 Pairs (莫队算法)

    problem_id=1551">题目链接 题目大意: 给出的询问,求出这个区间的里 差小于等于 2 的数字的对数. 思路分析: 莫队算法. 然后分析一下. 假设添加了一个数字.那么就 ...