c++数据结构中图的创建和查找最短路径

这个是图的创建和查找路径,有什么问题可以一起交流,本人qq921064167,当然也可以私信或者评论区留言,有什么有难度的题也可以向我询问,虽然我也不一定会,ok.

#include<iostream>
#define maxsize 1024
using namespace std;
//边的定义
typedef struct _EdgeNode {
	int weight;
	char adjvex;
	_EdgeNode* next;
}EdgeNode;
//节点的定义
typedef struct _VertexNode {
	char data;
	_EdgeNode* first;
}VertexNode;
//图的定义
typedef struct _AdjListGraph {
	VertexNode* adjlist;
	int vex;
	int edge;
}AdjListGraph;
//返回节点所在位置
bool visited[maxsize] = { 0 };//记录数据是否被访问

int Location(AdjListGraph& map,char c) {
	for (int i = 0; i < map.vex; i++) {
		if (map.adjlist[i].data == c) {
			return i;
		}
	}
	return -1;
}
//初始化图
bool initmap(AdjListGraph& map) {
	map.adjlist = new VertexNode[maxsize];
	map.edge = 0;
	map.vex = 0;
	for (int i = 0; i < maxsize; i++) {
		visited[i] = false;
	}
	return true;
}
//图的创建
bool creatmap(AdjListGraph& map) {
	cout << "请输入该图的顶点数以及边数:" << endl;
	cin >> map.vex >> map.edge;
	cout << "请输入相关顶点:" << endl;
	for (int i = 0; i < map.vex; i++) {
		cin >> map.adjlist[i].data;
		map.adjlist[i].first = NULL;
	}
	cout << "请输入想关联边的顶点及权重:" << endl;
	for (int i = 0; i < map.edge; i++) {
		char a1 = 0;//中间商
		char a2 = 0;
		int weight=0;//权重
		cin >> a1 >> a2;
		cin >> weight;
		int q1 = Location(map, a1);
		int q2 = Location(map, a2);
		if (q1 == -1 || q2 == -1) {
			cout << "找不到相关位置!" << endl;
			return false;
		}
		_EdgeNode* node = new EdgeNode;
		node->adjvex = a2;
		node->weight = weight;
		node->next = map.adjlist[q1].first;
		map.adjlist[q1].first = node;
	}
	return true;
}

int path[maxsize] = { 0 };
int minpath[maxsize] = { 0 };
int min_weight = 0x7fffffff;
int step = 0;

void DFS(AdjListGraph& map,int start,int end,int weights) {
	cout << "jinqu" << endl;
	int cur = -1;
	cur = start;
	if (cur == end) {
		cout << "jinqu=end" << endl;
		for (int i = 0; i < step; i++) {
			cout << map.adjlist[path[i]].data << " " << endl;
		}
		cout << "该路径对应的长度:" << weights << endl;
		if (min_weight > weights) {
			min_weight = weights;
			memcpy(minpath, path, step * sizeof(int));
			cout << "更新了一次最短路径" << endl;
		}
	}
		visited[start] = true;
		EdgeNode* edge = map.adjlist[cur].first;
		cout << "kuai yao xun huan le " << endl;
		while (edge) {
			cout << "yunxing*********" << endl;
			int weight = edge->weight;
			cur = Location(map,edge->adjvex);
			if (visited[cur] == false) {
				visited[cur] = true;
				path[step++] =cur;
				DFS(map,cur, end, weights + weight);
				visited[cur] = false;
				path[--step] = 0;
			}
			edge = edge->next;
		}

	
}

int main() {
	AdjListGraph G;
	//初始化
	initmap(G);
	//创建图
	creatmap(G);
	char start, end;
	cout << "请输入要查询的最短路径的起点和终点:" << endl;
	cin >> start >> end;
	//求两点间的最短路径
	DFS(G, Location(G, start), Location(G, end), 0);
	cout << "最小路径长度为:" <<min_weight << endl;
	cout << "路径:";
	int i = 0;
	while (i < maxsize && minpath[i]>0) {
		cout << G.adjlist[minpath[i]].data << " ";
		i++;
	}
	cout << endl;
	system("pause");
	cout << "贼牛p" << endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值