资源简介
C++ 数据结构 图,用迪杰斯特拉算法求最短路径,用DFS求旅游路线
代码片段和文件信息
#include “Graph.h“
#include
using namespace std;
CGraph::CGraph()
{
}
CGraph::~CGraph()
{
}
void CGraph::Init()
{
// 插入顶点
FILE *fVex = fopen(“Vex.txt“ “r“);
if (!fVex)
{
cout << “打开文件失败“ << endl;
return;
}
fscanf(fVex “%d“ &m_nVexNum);
Vex vex;
while (!feof(fVex))
{
fscanf(fVex “%d“ &vex.num);
fscanf(fVex “%s“ &vex.name);
fscanf(fVex “%s“ &vex.desc);
if (false == InsertVex(vex))
{
cout << “插入顶点失败“ << endl;
break;
}
}
fclose(fVex);
// 插入边
FILE *fEdge = fopen(“Edge.txt“ “r“);
if (!fEdge)
{
cout << “打开文件失败“ << endl;
return;
}
Edge edge;
for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
m_aAdjMatrix[i][j] = 0;
while (!feof(fEdge))
{
fscanf(fEdge “%d“ &edge.vex1);
fscanf(fEdge “%d“ &edge.vex2);
fscanf(fEdge “%d“ &edge.weight);
if (false == InsertEdge(edge))
{
cout << “插入边失败“ << endl;
break;
}
}
fclose(fEdge);
}
bool CGraph::InsertVex(Vex sVex)
{
m_aVexs[sVex.num] = sVex;
return true;
}
bool CGraph::InsertEdge(Edge sEdge)
{
m_aAdjMatrix[sEdge.vex1][sEdge.vex2] = sEdge.weight;
m_aAdjMatrix[sEdge.vex2][sEdge.vex1] = sEdge.weight;
return true;
}
Vex CGraph::GetVex(int nVex)
{
return m_aVexs[nVex];
}
int CGraph::FindEdge(int nVex Edge aEdge[])
{
int k = 0;
for (int i = 0; i < m_nVexNum; i++)
{
if (m_aAdjMatrix[nVex][i] != 0)
{
aEdge[k].vex1 = nVex;
aEdge[k].vex2 = i;
aEdge[k].weight = m_aAdjMatrix[nVex][i];
k++;
}
}
return k;
}
int CGraph::GetVexNum()
{
return m_nVexNum;
}
void CGraph::DFSTraverse(int nVex PathList &List)
{
int nIndex = 0;
bool aVisited[20] = { false };
DFS(nVex aVisited nIndex List);
}
int CGraph::FindShortPath(int nVexStart int nVexEnd Edge aPath[])
{
bool aVisited[20] = { false };
aVisited[nVexStart] = true;
Edge aEdge[20];
int min;
int d[20];
int num_edge = 0;
for (int i = 0; i < m_nVexNum; i++)
{
if (m_aAdjMatrix[nVexStart][i] == 0)
d[i] = INFINITE;
else
{
d[i] = m_aAdjMatrix[nVexStart][i];
aEdge[num_edge].vex1 = nVexStart;
aEdge[num_edge].vex2 = i;
aEdge[num_edge].weight = d[i];
num_edge++;
}
}
for (int i = 0; i < m_nVexNum - 1 && !aVisited[nVexEnd]; i++)
{
min = INFINITE;
int k;
for (int j = 0; j < m_nVexNum; j++)
if (!aVisited[j] && d[j] < min)
{
k = j;
min = d[j];
}
aVisited[k] = true;
for (int j = 0; j < m_nVexNum; j++)
if (!aVisited[j] && d[j] > d[k] + (m_aAdjMatrix[k][j] == 0 ? INFINITE : m_aAdjMatrix[k][j]))
{
d[j] = d[k] + m_aAdjMatrix[k][j];
aEdge[num_edge].vex1 = k;
aEdge[num_edge].vex2 = j;
aEdge[num_edge].weight = m_aAdjMatrix[k][j];
num_edge++;
aEdge[num_edge].vex1 = j;
}
}
num_edge--;
int nIndex = 0;
Edge aReserve[20];
for (int i = num_edge; i >= 0; i--)
if (aEdge[i].vex2 == nVexEnd)
{
aReserve[0].vex1 = a
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-06-14 20:35 GraphCPro\
目录 0 2018-04-21 12:17 GraphCPro\.vs\
目录 0 2018-04-21 12:17 GraphCPro\.vs\GraphCPro\
目录 0 2018-04-21 12:17 GraphCPro\.vs\GraphCPro\v14\
文件 37376 2018-06-14 20:35 GraphCPro\.vs\GraphCPro\v14\.suo
目录 0 2018-04-23 03:47 GraphCPro\Debug\
文件 56832 2018-05-01 23:07 GraphCPro\Debug\GraphCPro.exe
文件 863164 2018-05-01 23:07 GraphCPro\Debug\GraphCPro.ilk
文件 1142784 2018-05-01 23:07 GraphCPro\Debug\GraphCPro.pdb
目录 0 2018-05-01 23:07 GraphCPro\GraphCPro\
文件 1309 2018-04-21 12:17 GraphCPro\GraphCPro.sln
文件 8278016 2018-06-14 20:35 GraphCPro\GraphCPro.VC.db
目录 0 2018-05-01 23:07 GraphCPro\GraphCPro\Debug\
文件 65602 2018-04-24 01:08 GraphCPro\GraphCPro\Debug\Graph.obj
文件 64856 2018-04-23 23:25 GraphCPro\GraphCPro\Debug\graph.obj.enc
文件 199 2018-05-01 23:07 GraphCPro\GraphCPro\Debug\GraphCPro.log
目录 0 2018-05-01 23:07 GraphCPro\GraphCPro\Debug\GraphCPro.tlog\
文件 2220 2018-05-01 23:07 GraphCPro\GraphCPro\Debug\GraphCPro.tlog\CL.command.1.tlog
文件 39438 2018-05-01 23:07 GraphCPro\GraphCPro\Debug\GraphCPro.tlog\CL.read.1.tlog
文件 3328 2018-05-01 23:07 GraphCPro\GraphCPro\Debug\GraphCPro.tlog\CL.write.1.tlog
文件 207 2018-05-01 23:07 GraphCPro\GraphCPro\Debug\GraphCPro.tlog\GraphCPro.lastbuildstate
文件 1538 2018-05-01 23:07 GraphCPro\GraphCPro\Debug\GraphCPro.tlog\li
文件 3282 2018-05-01 23:07 GraphCPro\GraphCPro\Debug\GraphCPro.tlog\li
文件 782 2018-05-01 23:07 GraphCPro\GraphCPro\Debug\GraphCPro.tlog\li
文件 51623 2018-04-23 22:38 GraphCPro\GraphCPro\Debug\Main.obj
文件 67490 2018-05-01 23:07 GraphCPro\GraphCPro\Debug\Tourism.obj
文件 445440 2018-05-01 23:07 GraphCPro\GraphCPro\Debug\vc140.idb
文件 364544 2018-05-01 23:07 GraphCPro\GraphCPro\Debug\vc140.pdb
文件 91 2018-04-21 12:24 GraphCPro\GraphCPro\Edge.txt
文件 5312 2018-04-24 01:08 GraphCPro\GraphCPro\Graph.cpp
文件 941 2018-04-23 22:38 GraphCPro\GraphCPro\Graph.h
............此处省略6个文件信息
- 上一篇:21天学通 C++第8版)完整高清
- 下一篇:自制杀毒软件C++源码
相关资源
- 自制杀毒软件C++源码
- 21天学通 C++第8版)完整高清
- 数据结构 C语言描述 第2版 殷人昆 随
- More Effective C++ 侯捷翻译 高清带书签
- 高速上手 C++ 11 14 17
- c++21点游戏 命令行界面
- C++程序编写的打地鼠游戏
- c++ 代码重用教程
- C_C++程序员面试宝典 文字版 非扫描
- 数据结构C语言版 严蔚敏 清华大学出
- 求图中任意两点的最短路径和全部路
- Design Patterns in Modern C++-Apress(2018)
- Visual C++ 2010 Express 中文版安装文件 器
- 数字图像处理MFC编程之灰度图像线性
- 0-9手写数字识别C++实现
- 在线考试系统 + C++ +access
- C++ MFC 编写的聊天室软件源码
- 4X4X4光立方资料含电路图,制作方法,
- C++编程思想C++编程思想
- linux libstdc++.so.6库
- C++语言程序设计学生用书[aOkREm]
- 基于C语言和SQL SERVER数据库实现的图书
- C++经典程序100例
- Problem Solving with C++ 10th Global Edition
- WIN32程序的C++编程
- C++编写五子棋带AI,mfc,附教程
- C++遗传算法解决柔性作业车间调度附
- 基于VC++的包过滤防火墙论文+源码
- C++程序设计思想与方法课后答案
- C++调用C#dll代码
评论
共有 条评论