Kruskal和prime算法的类实现,图的遍历BFS算法。
一.图的遍历
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int n, m; //行数和列数
const int maxn = 100;
char g[maxn][maxn]; //图
bool vis[maxn][maxn]; //访问标记数组,false表示点没有被访问过
int disx[4] = { 0,0,1,-1 }; //四个
int disy[4] = { 1,-1,0,0 }; //方向
int dx, dy; //起点位置
int counter; //黑瓷砖的数目
bool flag = false; //判断是否有起点
struct node {
int x, y;
}p[maxn];
queue<node> que;
vector<node> vec;
void graph(int n, int m) {
cout << "请输入行数和列数 :";
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> g[i][j];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (g[i][j] == '@') {
dx = i; dy = j;
flag = true;
break;
}
}
if (flag == true) break;
}
if (flag) {
cout << "起始点的坐标是";
printf("( %d , %d )\n\n", dx, dy);
}
else cout << "没有起点" << endl;
}
void bfs() {
vec.clear();
que.empty();
memset(vis, false, sizeof vis);
graph(n, m);
if (!flag) {
counter = 0;
cout << "黑瓷砖的数目是" << counter << endl;
}
else {
counter = 1;
node point;
point.x = dx, point.y = dy;
que.push(point);
vis[point.x][point.y] = true;
vec.push_back(point);
while (!que.empty()) {
node temp = que.front();
node next;
que.pop();
for (int i = 0; i < 4; ++i) {
int x = temp.x + disx[i];
int y = temp.y + disy[i];
next.x = x; next.y = y;
if (g[next.x][next.y] == 'b'&&vis[next.x][next.y] == false) {
vis[next.x][next.y] = true;
que.push(next); ++counter;
vec.push_back(next);
}
}
}
cout << "黑瓷砖的数目是" << counter << endl;
cout << endl;
int number = 0;
cout << "且到达每个黑瓷砖的顺序是 :" << endl;
for (int i = 0; i < vec.size(); ++i) {
if (i != vec.size() - 1) {
++number;
printf("( %d , %d)", vec[i].x, vec[i].y);
cout << " -> ";
if (number % 5 == 0) cout << endl;
}
else printf("( %d , %d)\n", vec[i].x, vec[i].y);
}
}
}
int main() {
bfs();
return 0;
}
糟糕的上机,prim似乎有bug
二.求无向连通图的生成树
#pragma once
#include<algorithm>
#include <fstream>
#include<iostream>
using namespace std;
const int inf = 1000000000;
int p[100];
int maze[100][100];
template<class T>
struct Edge
{
int u, v;
int net;
T key;
Edge() :u(-1), v(-1), key(0) {}
};
template<class T>
class MST {
protected:
Edge<T>*edge;
int maxSize, e;
int head[100];
public:MST(int sz = 100) :maxSize(sz), e(0) {
edge = new Edge<T>[sz];
memset(head, -1, sizeof(head));
}
void addedge(Edge<T>& item);
void establish();
int Kruskal();
int Prim(int n);
};
template<class T>
void MST<T>::addedge(Edge<T>& item)
{
int frm = item.u, to = item.v, w = item.key;
edge[e].u = frm;
edge[e].v = to;
edge[e].key = w;
edge[e].net = head[frm];
head[frm] = e++;
edge[e].u = to;
edge[e].v = frm;
edge[e].key = w;
edge[e].net = head[to];
head[to] = e++;
}
template<class T>
void MST<T>::establish()
{
for (int i = 0; i < 100; i++)
for (int j = 0; j< 100; j++)
if (i == j)maze[i][j] = 0;
else maze[i][j] = inf;
int u, v, w;
Edge<int> temp;
while (cin >> u >> v >> w) {
if (u == 0 && v == 0 && w == 0)break;
temp.u = u, temp.v = v, temp.key = w;
maze[u][v] = maze[v][u] = w;
addedge(temp);
}
return;
}
bool cmp(Edge<int> a, Edge<int> b)
{
return a.key < b.key;
}
int find(int x)
{
return p[x] == x ? x : p[x] = find(p[x]);
}
template<class T>
int MST<T>::Kruskal()
{
ofstream outfile;
outfile.open("outdata.txt");
outfile << "Kruskal计算如下:\n";
int ans = 0;
for (int i = 0; i < 50; i++)p[i] = i;
sort(edge, edge + e, cmp);
for (int i = 0; i < e; i++) {
int x = find(edge[i].u);
int y = find(edge[i].v);
if (x != y) { ans += edge[i].key;
// cout << edge[i].u << " " << edge[i].v << endl;
outfile << edge[i].u << " " << edge[i].v<<" 权值: " << edge[i].key << endl;
p[x] = y; }
}
outfile << "最小生成树中长度: " << ans << endl;
outfile.close();//关闭文件,保存文件。
return ans;
}
Edge<int> dis[100];
int path[100];
template<class T>
int MST<T>::Prim(int n)
{
bool vis[100];
ofstream outfile;
outfile.open("outdata.txt", ios::binary | ios::app | ios::in | ios::out);
outfile << "Prim计算如下:\n\n\n";
//int dis[100];
for (int i = 0; i < 100; i++)
dis[i].key = inf;
memset(vis, 0, sizeof(vis));
int ans = 0;
dis[3].key = 0;
while (1)
{
int k = 0;
for (int j = 1; j <= n; j++)
{
if (!vis[j] && dis[j].key<dis[k].key) //这一步找未收录顶点中dist值最小的
k = j;
}
if (!k) break;
vis[k] = 1;
ans += dis[k].key;
if (dis[k].u > 0)
outfile <<" "<<dis[k].u << " " << dis[k].v << " 权值" << dis[k].key << "\n" << endl;
for (int j = 1; j <= n; j++)
{
if (dis[j].key>maze[k][j])
{
dis[j].key = maze[k][j];
dis[j].u = k;
dis[j].v = j;
path[j] = k;
}
}
}
outfile << "总长度 " << ans << endl;
outfile.close();//关闭文件,保存文件。
return ans;
}
#include<iostream>
#include"graph.h"
using namespace std;
int main()
{
MST<int> m;
int n;
cin >> n;
m.establish();
m.Kruskal();
m.Prim(n);
getchar();
return 0;
}
Kruskal和prime算法的类实现,图的遍历BFS算法。的更多相关文章
- 图的遍历BFS广度优先搜索
图的遍历BFS广度优先搜索 1. 简介 BFS(Breadth First Search,广度优先搜索,又名宽度优先搜索),与深度优先算法在一个结点"死磕到底"的思维不同,广度优先 ...
- 图的遍历BFS
图的遍历BFS 广度优先遍历 深度优先遍历 可以进行标记 树的广度优先遍历,我们用了辅助的队列 bool visited[MAX_VERTEX_NUM] //访问标记数组 //广度优先遍历 void ...
- 图的遍历——BFS
原创 裸一篇图的BFS遍历,直接来图: 简单介绍一下BFS遍历的过程: 以上图为例子,从0开始遍历,访问0,按大小顺序访问与0相邻的所有顶点,即先访问1,再访问2: 至此顶点0已经没有作用了,因为其本 ...
- 模板 图的遍历 bfs+dfs 图的最短路径 Floyed+Dijkstra
广搜 bfs //bfs #include<iostream> #include<cstdio> using namespace std; ],top=,end=; ][]; ...
- 图的遍历——BFS(队列实现)
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> ...
- 图之强连通--Tarjan算法
强连通分量 简介 在阅读下列内容之前,请务必了解图论基础部分. 强连通的定义是:有向图 G 强连通是指,G 中任意两个结点连通. 强连通分量(Strongly Connected Components ...
- C#与数据结构--图的遍历
http://www.cnblogs.com/abatei/archive/2008/06/06/1215114.html 8.2 图的存储结构 图的存储结构除了要存储图中各个顶点的本身的信息外,同时 ...
- 算法笔记_144:有向图强连通分量的Tarjan算法(Java)
目录 1 问题描述 2 解决方案 1 问题描述 引用自百度百科: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连 ...
- tarjan算法,一个关于 图的联通性的神奇算法
一.算法简介 Tarjan 算法一种由Robert Tarjan提出的求解有向图强连通分量的算法,它能做到线性时间的复杂度. 我们定义: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly ...
随机推荐
- 【Linux】磁盘读写 测试
一.如何查看当前磁盘的IO使用情况 使用命令:iotop Total DISK READ: 3.89 K/s | Total DISK WRITE: 0.00 B/s TID PRIO USER DI ...
- iOS实现pdf文件预览,上下翻页、缩放,读取pdf目录
最近有个朋友想做一个pdf预览,要求能够上下滑动翻页.带缩放.目录跳转功能. 因为之前我只做过简单的预览,那时直接用uiwebview实现的,这次找了下资料,发现一个比较好的库. 其原理实现: 自定义 ...
- 【Zend Studio】在Zend Studio中调试ThinkPhp框架
在这篇文章中,笔者将会展示如何在Zend Studio下进行断点调试Think PHP.环境:windows 7.wampServer 3.1.4 64bit.zend studio 13.6.1Th ...
- Linux内核剖析(二)Linux内核绪论
什么是内核 内核是操作系统最基本的部分.它是为众多应用程序提供对计算机硬件的安全访问的一部分软件,这种访问是有限的,并且内核决定一个程序在什么时候对某部分硬件操作多长时间.内核的分类可分为单内核和双内 ...
- Nginx的location匹配规则
一 Nginx的location语法 location [=|~|~*|^~] /uri/ { … } = 严格匹配.如果请求匹配这个location,那么将停止搜索并立即处理此请求 ...
- HLS playlist典型示例
[时间:2018-06] [状态:Open] [关键词:流媒体,HLS,m3u8,playlist,variant, alternate] 0 引言 本文主要是对apple官网上的echnical N ...
- c# async和await 用法(阻塞与不阻塞)
void PagePaint() { Console.WriteLine("Paint Start"); Paint(); Console.WriteLine("Pain ...
- Linux连接redis客户端出现Could not connect to Redis at 127.0.0.1:6379: Connection refused
打开两个窗口,一个执行 命令,另外一个窗口执行
- golang加油!
- 加入ffmpeg播放视屏
下面的字反了..,另外没声音 2018-4-28 前段时间已经做的差不多了,音频的pack取出来用openAL播放,并实现了视屏同步播放,并且支持unity 现在的问题就是支持大分辨率视屏播放的问题, ...