最小生成树算法。这里的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. CentOS6.8 编译安装LNMP

    思路:根据Linux系统以及公司网站系统的信息,选择合适的安装包进行安装 一.查看系统信息 # uname -a # 查看内核/操作系统/CPU信息 # /etc/issue # 查看操作系统版本 # ...

  2. NSMutableAttributedString 的使用方法,设置格式

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:title]; NS ...

  3. PHP使用微软认知服务Face API

    下面主要介绍基于PHP语言,基于guzzle类库,调用微软最新推出的认知服务:人脸识别. 实验环境: IDE:Eclipse for PHP Developers Version: Neon.1 Re ...

  4. Spring 应用外部属性文件 配置 context 错误

    在Spring配置文件中出现通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明这个错误,其实主要是我们在引入命名空间时没有正确引入它的DTD解析 ...

  5. android studio改动module名称

    新建一个android studio项目,默认Module名称是app 右键app选择Rename,或者Shift + F6也能够.重命名module名称 重命名为abc之后中,如图上面箭头所指的ap ...

  6. Spark高速上手之交互式分析

    1.1  Spark交互式分析 执行Spark脚本前,启动Hadoop的HDFS和YARN.Spark的shell提供 了简单方式去识别API.相同也有一个强大的工具去交互式地分析数据. 两种语言有这 ...

  7. OpenGL/GLSL数据传递小记(2.x)(转)

    本篇记录一下关于OpenGL程序中绑定各种GLSL变量的一些注意问题(有些是近期编写代码感受强烈的).以供参考.——ZwqXin.com 本文来源于 ZwqXin (http://www.zwqxin ...

  8. 机器学习10—K-均值聚类学习笔记

    机器学习实战之K-Means算法 test10.py #-*- coding:utf-8 import sys sys.path.append("kMeans.py") impor ...

  9. Python环境搭建及IDE选择(转载)

    Python环境搭建及IDE选择 人工智能社区 http://studyai.com 系统:Windows 7 版本:Python 2.7 一.安装Python 在开始编程之前,我们首先需要搭建Pyt ...

  10. 使用Erlang实现简单的排序算法:快速排序,冒泡排序,插入排序

    [排序算法] -module(sort). -compile(export_all). %%快速排序 qsort([]) -> []; qsort([Pivot|T]) -> qsort( ...