Data Structure Graph: prim
最小生成树算法。这里的s是可以随意选取的,不影响树的生成,但是不同的s有不同的dis
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
using namespace std; const int MAX = ; const int N = ;
int dis[N];
vector<bool> visit(N);
int graph[N][N] = {{, , , , },
{, , , , },
{, , , , },
{, , , , },
{, , , , }}; void prim() {
int s = ;
visit[s] = true;
dis[s] = ;
for (int i = ; i < N; i++) {
int min_dis = MAX;
int u;
for (int i = ; i < N; i++) {
if (!visit[i] && graph[s][i]) dis[i] = min(dis[i], dis[s] + graph[s][i]);
if (!visit[i] && dis[i] < min_dis) {
u = i;
min_dis = dis[i];
}
}
s = u;
visit[s] = true;
}
} int main() {
memset(dis, MAX, sizeof(dis));
prim();
for (int i = ; i < N; i++) cout << dis[i] << endl;
return ;
}
Data Structure Graph: prim的更多相关文章
- Data Structure Graph: strong connectivity
如果为undirected graph就是dfs或者bfs,如果都能visit则为连通O(V+E). 如果为directed graph就先dfs或者bfs,再reverse direct,再dfs或 ...
- Data Structure Graph: cycle in a directed graph
geeks上的解答复杂了些,用回溯就行了 #include <iostream> #include <vector> #include <algorithm> #i ...
- [Algorithm] JavaScript Graph Data Structure
A graph is a data structure comprised of a set of nodes, also known as vertices, and a set of edges. ...
- HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...
- [转]Data Structure Recovery using PIN and PyGraphviz
Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
随机推荐
- jenkins调用shell脚本 输出带颜色字体
jenkins需要安装AnsiColor插件在构建环境项选择“color ansi console output” 安装插件AnsiColor shell 脚本相关颜色设置 echo -e " ...
- ganlia安装配置文档
gangliaz在ubuntu中安装和配置很简单 1. 服务器端安装 sudo apt-get install ganglia-monitor ganglia-webfrontend rrdtool ...
- php单元测试入门教程phpunit详解
本文档提供了一些phpunit官方教程没有提到的信息,帮助初学者快速了解php单元测试,在phpunit官网提供了详细的中文教程,可选多种格式下载 phpunit官网地址:https://phpuni ...
- Linux系统控制文件 /etc/sysctl.conf详解
/etc/sysctl.conf这个目录主要是配置一些系统信息,/etc/sysctl.conf参数解释: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...
- 跟我一起写 Makefile(三)[转]
原文链接 http://bbs.chinaunix.net/thread-408225-1-1.html(出处: http://bbs.chinaunix.net/) make 的运行—————— 一 ...
- oracle中sqlldr工具使用时注意事项
1.命令写在一行:如,sqlldr sh/&sh_pass@&connect_string control=&ctl_file data=&dat_file log=& ...
- (1)安装kvm
我的环境是redhat虚拟机,版本信息如下: [root@localhost ~]# cat /etc/issue Red Hat Enterprise Linux Server release 6. ...
- git学习之创建版本库(三)
创建版本库 什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以 ...
- Pexpect--example--hive.py解读
python version 2.6.6 ; pexpect 2.3 login方法解读: def login (args, cli_username=None, cli_password=None) ...
- ubuntu下搭建的lamp环境新建站点
这几天刚装了一个ubuntu 16.04桌面版,总之来来回回几遍才基本把环境搭建好,本来用apt-get搭建,结果不知道什么原因16.04版不支持装php5 ,提示源放弃了php5版本,不得不使用ph ...