最小生成树算法。这里的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. DbVisualizer出现下列错误:Could not read XML file

    数据库连接工具对于我们软件测试工程师来说是日常的工具,一旦关键时刻报些莫名的错误,相信大家很是苦恼.当然,各公司数据库连接工具各异,像DbVisualizer.PLsql.sqldeveloper.T ...

  2. jmeter压测-负载配置

    jmeter 压测 一般压测的时间是10-15分钟 TPS:服务端每秒钟处理的请求数  越大越好 响应时间 :越短越好 并发用户数  也就是多少并发 指标给你:tps要达到多少 响应时间要达到多少 并 ...

  3. 会话管理之Cookie技术

    会话管理是web开发中比较重要的环节,这一节主要总结下会话管理中的cookie技术. 1. 何为会话 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个 ...

  4. MySQL_知识点

    1.执行顺序 语句模板: (8)SELECT (9)DISTINCT (11)<Top Num> <select list> (1)FROM [left_table] (3)& ...

  5. kill 命令

    Linux中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以使用Ctrl+C键,但是,对于一个后台进程就须 ...

  6. Struts 2 框架特性

    POJO形式和POJO动作 - 已经摆脱了Struts2的动作表单的Struts框架的一个组成部分.Struts2可以使用任何的POJO接收的形式输入.同样的,你现在可以看到任何POJO的Action ...

  7. 线程池 http请求

    package com.aibi.cmdc.test; import java.io.BufferedReader; import java.io.InputStream; import java.i ...

  8. display getSize()

    If you want the the display dimensions in pixels you can use getSize: Display display = getWindowMan ...

  9. thinkPHP5.0的学习研究【序言】

    2017年6月19日13:19:151.ThinkPHP V5.0——为API开发而设计的高性能框架2.ThinkPHP是一个免费开源的,快速.简单的面向对象的轻量级PHP开发框架,是为了敏捷WEB应 ...

  10. 读《《图解TCP-IP》》有感

    读<<图解TCP/IP>>有感 TCP/IP 近期几天读完<<图解TCP/IP>>,收获蛮多,记得上学时读stevens的<<TCP/IP具 ...