POJ1258 基础最小生成树
本文出自: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 基础最小生成树的更多相关文章
- POJ1258 (最小生成树prim)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 46319 Accepted: 19052 Descri ...
- hdu1102 Constructing Roads 基础最小生成树
//克鲁斯卡尔(最小生成树) #include<cstdio> #include<iostream> #include<algorithm> using names ...
- poj1258 Agri-Net 最小生成树
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44032 Accepted: 18001 Descri ...
- hdu1162 Eddy's picture 基础最小生成树
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...
- hdu1301 Jungle Roads 基础最小生成树
#include<iostream> #include<algorithm> using namespace std; ; int n, m; ]; struct node { ...
- hdu1879 继续畅通工程 基础最小生成树
#include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> u ...
- hdu1875 畅通工程再续 暴力+基础最小生成树
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; ; ; ; in ...
- hdu1863 畅通工程 基础最小生成树
#include <iostream> #include <cstdio> #include <algorithm> #define N 110 #define M ...
- hdu1233 还是畅通工程 基础最小生成树
//克鲁斯卡尔 #include<iostream> #include<algorithm> using namespace std; ; struct node { int ...
随机推荐
- git bash 出现vim的时候怎么退出
如果是输出状态,首先Esc退出输入状态,然后Shift+;,再输入q!或wq!(不保存改动,wq!是保存文件的写入修改)退出
- Microsoft Toolkit 2.5下载 – 一键激活Windows 8.1/2012 R2/Office 2013
http://www.dayanzai.me/microsoft-toolkit-2-5.html
- AsyncSocket长连接棒包装问题解决
project正在使用长连接快来server沟通.因此,指定我们的协议前两个字节为数据长度来区分数据包 app这边数据有两种传输形式: 1.app主动请求所须要的数据: 2.app异步接收来自服务端的 ...
- DuiVision开发教程(15)-DUI文本控制基础类
CControlBaseFont类是DuiVision支持所有基类的控件的文本属性. 此控件例如属性列表,下面: 物业名称 类型 说明 title 字符串 控件的显示标题 font 字体 控件的字体, ...
- JAVA设计模式--辛格尔顿
Singleton模式可以作为一种编程技术,让我们先从理论上说代码 单例模式三个关键点: 1).某个类仅仅能有一个实例 2).该类必须自行创建这个实例 3).该类必须自行向整个系统提供这个实例 应用场 ...
- codeigniter 该脚本在运行300s超时退
直接看代码, file:system/core/CodeIgniter.php /* 102 * -------------------------------------------------- ...
- Quartz.net开源作业调度
Quartz.net开源作业调度框架使用详解 前言 quartz.net作业调度框架是伟大组织OpenSymphony开发的quartz scheduler项目的.net延伸移植版本.支持 cron- ...
- redhat6.3已安装was6.1你可以不弹出安装程序
这在为期两天的课程redhat6.3安装was6.1 使用Xmanager打开图形界面.进入/WAS夹,跑./install 它有一个直接跳转,不管是什么反应,起初我以为这个问题的图形界面,搜索了半天 ...
- mysql 解压缩和赋权
拉开拉链mysql紧凑根文件夹 注意ini配置文件的内容 basedir = D:\mysql-5.6.17-winx64 datadir = D:\mysql-5.6.17-winx64 por ...
- JAVA —— 数组
import java.util.Arrays; public class Array { public static void main(String[] args){ Array test= ...