最小生成树算法。这里的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的更多相关文章

  1. Data Structure Graph: strong connectivity

    如果为undirected graph就是dfs或者bfs,如果都能visit则为连通O(V+E). 如果为directed graph就先dfs或者bfs,再reverse direct,再dfs或 ...

  2. Data Structure Graph: cycle in a directed graph

    geeks上的解答复杂了些,用回溯就行了 #include <iostream> #include <vector> #include <algorithm> #i ...

  3. [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. ...

  4. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

  5. [转]Data Structure Recovery using PIN and PyGraphviz

    Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...

  6. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  7. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  8. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  9. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

随机推荐

  1. jenkins调用shell脚本 输出带颜色字体

    jenkins需要安装AnsiColor插件在构建环境项选择“color ansi console output” 安装插件AnsiColor shell 脚本相关颜色设置 echo -e " ...

  2. ganlia安装配置文档

    gangliaz在ubuntu中安装和配置很简单 1.  服务器端安装 sudo apt-get install ganglia-monitor ganglia-webfrontend rrdtool ...

  3. php单元测试入门教程phpunit详解

    本文档提供了一些phpunit官方教程没有提到的信息,帮助初学者快速了解php单元测试,在phpunit官网提供了详细的中文教程,可选多种格式下载 phpunit官网地址:https://phpuni ...

  4. 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 ...

  5. 跟我一起写 Makefile(三)[转]

    原文链接 http://bbs.chinaunix.net/thread-408225-1-1.html(出处: http://bbs.chinaunix.net/) make 的运行—————— 一 ...

  6. oracle中sqlldr工具使用时注意事项

    1.命令写在一行:如,sqlldr sh/&sh_pass@&connect_string control=&ctl_file data=&dat_file log=& ...

  7. (1)安装kvm

    我的环境是redhat虚拟机,版本信息如下: [root@localhost ~]# cat /etc/issue Red Hat Enterprise Linux Server release 6. ...

  8. git学习之创建版本库(三)

    创建版本库 什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以 ...

  9. Pexpect--example--hive.py解读

    python version 2.6.6 ; pexpect 2.3 login方法解读: def login (args, cli_username=None, cli_password=None) ...

  10. ubuntu下搭建的lamp环境新建站点

    这几天刚装了一个ubuntu 16.04桌面版,总之来来回回几遍才基本把环境搭建好,本来用apt-get搭建,结果不知道什么原因16.04版不支持装php5 ,提示源放弃了php5版本,不得不使用ph ...