自己动手实现最短路径——附源码和程序


从第一次构想写最短路径,到现在过了差不多将近一个月左右了,才完成了最短路径算法,其实只是闲着没事做,提升一下自己的能力。再来看一下这个图,个人觉得这种简洁的UI不错(其实是懒得多想),这种给了用户发展空间,自己想要多少点就可以有多少。

程序运行结果图
这次语言选择C++加上easyx图形库,开发环境Visual Studio 2019

迪杰斯特拉算法

  1. 算法数据结构
  • 坐标
struct Coor {
	int x;
	int y;
};
  • 节点
//因为用户要自己决定多少个点
//所以使用动态分配
struct Node {
	
	char** a;
	
	Coor* coor;
};

typedef struct Graph {
	Node node;//定点表
	
	double** edg;
}Graph;
  • 所有函数
int address(Graph g, char *P)//查询地址下标
void CreateGraph(Graph* g)//初始化图
void SetWeight(Graph* g)//设置边的权重
void Dijkstra(Graph g)//迪杰斯特拉算法
int dis(Graph g, char* a, char* b)//返回两地之间的距离
  • 绘画函数
void lineArrow(Graph g)//画带箭头的线
void Pan(Graph *g, int row, int col)//对边值赋值并且判断
bool StartLine(Graph g)//起始点
bool EndLine(Graph &g) //终止点
  1. 在主函数中调用函数
    这里顺序要整理好,不然可能会出错
int main() 
{
	initgraph(900, 650);
	setbkcolor(WHITE);
	cleardevice();
	Graph g;
	int Htext = 30;//字体大小
	char s[10];
	setcolor(RED);
	outtextxy(0, 0, "绘图区域");
	rectangle(-10, -10, 600, 500);
	setbkmode(TRANSPARENT);
	settextstyle(Htext, 0, "华文楷体");
	outtextxy(610, 0, "地标个数:");
	Sleep(1000);
	InputBox(s, 10, "请输入数量");
	outtextxy(610,  Htext, s);
	outtextxy(610, Htext * 2, "地标名称:");
	num = atoi(s);
	
	分配内存
	g.node.a = new char* [num];
	for (int j = 0; j < num; j++)
	{
		g.node.a[j] = new char[50];
	}

	g.node.coor = new Coor[num];

	g.edg = new double* [num];
	for (int j = 0; j < num; j++)
	{
		g.edg[j] = new double[num];
	}
	/

	CreateGraph(&g);//初始化

	//输入相关信息
	for (int j = 0; j < num; j++)
	{
		InputBox(g.node.a[j], 10, "请输入地标名");
		outtextxy(610, Htext*(j+3), g.node.a[j]);
	}
	char startdi[50],endi[50];
	outtextxy(0, 510, "起始地:"); InputBox(startdi, 50, "请输入地标名"); outtextxy(textwidth("起始地:")+20, 510, startdi);
	outtextxy(0, 550, "目的地:"); InputBox(endi, 50, "请输入地标名"); outtextxy(textwidth("起始地:") + 20, 550, endi);
	///

	//画点
	setcolor(GREEN);
	bool flag = false; int i = 0; char val;
	MOUSEMSG m;		// 定义鼠标消息
	while (true)
	{
		// 获取一条鼠标消息
		m = GetMouseMsg();
		switch (m.uMsg)
		{
		case WM_LBUTTONDOWN:
			if (flag == false&&m.x<600-20&&m.y<500-20&&m.x>20&&m.y>20) {
				fillcircle(m.x, m.y, 20);
				
				outtextxy(m.x, m.y, g.node.a[i]);
				g.node.coor[i].x = m.x;
				g.node.coor[i].y = m.y;
				i++;
			}
			break;
		case WM_LBUTTONUP:
			if (flag)
				flag = !flag;
			break;
		}
		
		if (i == num)
			break;
	}
	
	//画线
	setcolor(BLUE);
	while (true)
	{
		StartLine(g);
		EndLine(g);
		lineArrow(g);
		sprintf(num_dist, "%d", int(distancess));
		outtextxy((g.node.coor[row].x + g.node.coor[column].x) / 2, (g.node.coor[row].y + g.node.coor[column].y) / 2, num_dist);
		Pan(&g, row, column);
		if (_kbhit())
		{
			val = getch();
			if (val == 27)
				break;
		}	
	}

	//计算最短路径
	Dijkstra(g);
	//输出
	setcolor(RED);
	outtextxy(0, 590, "最短距离为:");
	sprintf(num_dist, "%d", dis(g, startdi, endi));
	outtextxy(textwidth("最短距离为:") + 20, 590, num_dist);

	//释放
	for (int i = 0; i < num; i++)
		delete[]g.node.a[i];
	delete[]g.node.a;
	delete[]g.node.coor;
	for (int i = 0; i < num; i++)
		delete[]g.edg[i];
	delete[]g.edg;
	for (int i = 0; i < num; i++)
		delete[]distances[i];
	delete[]distances;
	/
	_getch();
	closegraph();

	return 0;
}
  1. 主要算法分析
for (m = 0; m < VertexNum; m++)
{
		if ((!used[m]) && (distances[0][m] < mini))
		{
			mini = distances[0][m];
			j = m;
		}
}
//其中j就是用来标记已经确定是最短路径的点的下标
//Mini是用来找寻最短距离的中间量
//这个条件是用来判断还没有确定的并且从第一个点可以到达其它点的下标
//整个循环是用来找出最短距离
//并且取得下标将其标记为已经确定最短路径
for (n = 0; n < VertexNum; n++)
{
	if ((!used[n]) && (distances[0][n] > distances[0][j] + g.edg[j][n]))
	{
		distances[0][n] = distances[0][j] + g.edg[j][n];
		parent[n] = j;
	}
}
//例:因为a到a,b已经确认所以看c
//a点不能直接到达点c
//经过上一个循环得出a到b有最短路径
//然后通过查询b到c的距离是否可达
//如果可达则用a到b的距离加上b到c的距离赋值给a到c的距离
  1. 完整代码
//DataStruct.h
#include<iostream>
#include<cmath>
#include<string>
using namespace std;

#define INFINITYS 65535

int** distances;//记录从源点到终点当前最短路径长度
int row, column, num;//行,列,点的个数
double distancess;//两点之间的距离
char num_dist[20];//距离转换中间量



/*测试用例
0 5 0 7 0 0
0 0 4 0 0 0
8 0 0 0 0 9
0 0 5 0 0 6
0 0 0 5 0 0
3 0 0 0 1 0
*/
struct Coor {
	int x;
	int y;
};
struct Node {
	
	char** a;
	
	Coor* coor;
};
typedef struct Graph {
	Node node;//定点表
	
	double** edg;
}Graph;
//查询地址下标
int address(Graph g, char *P) {
	for (int i = 0; i < num; i++) {
		if (strcmp(P, g.node.a[i])==0) {
			return i;
		}
	}
	return 0;
}

void CreateGraph(Graph* g) {
	int i = 0;
	int j = 0;
	
	for (i = 0; i < num; i++)
		for (j = 0; j < num; j++)
			g->edg[i][j] = 0;
}
//设置权重
void SetWeight(Graph* g) {
	int i, j;
	for (i = 0; i < num; i++) {
		for (j = 0; j < num; j++) {
			if (g->edg[i][j] == 0)
				g->edg[i][j] = INFINITYS;
		}
	}
}
//迪杰斯特拉算法
void Dijkstra(Graph g) {
	int VertexNum = num;
	int i, j, k, m, n, t;
	int mini;
	//记录从源点到终点是否已被确定最短路径长度,1表示确定,0表示尚未确定
	int* used = new int[VertexNum];
	
	
	distances = new int* [num];
	for (int j = 0; j < num; j++)
	{
		distances[j] = new int[num];
	}

	SetWeight(&g);
	for (t = 0; t < num; t++)
	{
		for (i = 0; i < VertexNum; i++) {
			used[i] = 0;
			
			distances[t][i] = g.edg[t][i];

		}
		used[t] = 1;
		/*cout << "初始化状态," << g.node.a[t] << "到其他各点的最短路径为:" << endl;
		for (k = 0; k < VertexNum; k++) {
			cout << distances[t][k] << '\t';
		}*/

		//cout << endl;
		for (i = 0; i < VertexNum; i++) {
			j = 0;
			mini = INFINITYS;
			//计算出最小的
			for (m = 0; m < VertexNum; m++)
			{
				if ((!used[m]) && (distances[t][m] < mini))
				{
					mini = distances[t][m];
					j = m;
				}
			}
			used[j] = 1;
			for (n = 0; n < VertexNum; n++)
			{
				if ((!used[n]) && (distances[t][n] > distances[t][j] + g.edg[j][n]))
				{
					distances[t][n] = distances[t][j] + g.edg[j][n];
					
				}
			}

			/*cout << "第" << i + 1 << "次循环之后," << g.node.a[t] << "到其他个点的最短路径为" << endl;
			for (k = 0; k < VertexNum; k++)
				cout << distances[t][k] << '\t';
			cout << endl;*/
		}

		//cout << endl;
	}
	delete[]used;
}

int dis(Graph g, char* a, char* b)
{
	/*cout << address(g, a) << '\t' << address(g, b) << endl;
	cout << distances[address(g, a)][address(g, b)];*/
	return distances[address(g, a)][address(g, b)];

}


//ShortRoad.cpp
#include <graphics.h>
#include <conio.h>
#include "DataStruct.h"
#include <stdlib.h>



void lineArrow(Graph g)
{
	int x1 = g.node.coor[row].x, y1 = g.node.coor[row].y, x2 = g.node.coor[column].x, y2 = g.node.coor[column].y;
	line(x1, y1, x2, y2);
	distancess = sqrt((y1 - y2) * (y1 - y2) + (x1 - x2) * (x1 - x2));
	double tmpx = double(x1 + (x2 - x1) * (1 - (12 * sqrt(3) / 2) / distancess));
	double tmpy = double(y1 + (y2 - y1) * (1 - (12 * sqrt(3) / 2) / distancess));
	if (y1 == y2)
	{
		line(x2, y2, int(tmpx), int(tmpy + 6));
		line(x2, y2, int(tmpx), int(tmpy - 6));
	}
	else
	{
		double k = (double(x2) - double(x1)) / (double(y1) - double(y2));
		double increX = 6 / sqrt(k * k + 1);
		double increY = 6 * k / sqrt(k * k + 1);
		line(x2, y2, int(tmpx + increX), int(tmpy + increY));
		line(x2, y2, int(tmpx - increX), int(tmpy - increY));
	}
}

void Pan(Graph *g, int row, int col) 
{
	if (row != col)
	 {
		g->edg[row][col] = int(distancess);
	 }
}

bool StartLine(Graph g)
{
	MOUSEMSG m;
	while (true)
	{
		m = GetMouseMsg();
		switch (m.uMsg)
		{
		case WM_LBUTTONDOWN:
			for (int i = 0; i < num; i++)
				if (m.x >= g.node.coor[i].x - 20 && m.x <= g.node.coor[i].x + 20 && m.y >= g.node.coor[i].y - 20 && m.y <= g.node.coor[i].y + 20)
				{
					row = i;
					return true;
				}
			break;
		default:
			break;
		}
	}
	return false;
}
bool EndLine(Graph &g) 
{
	MOUSEMSG m;
	while (true)
	{
		m = GetMouseMsg();
		switch (m.uMsg)
		{
		case WM_LBUTTONDOWN:
			for (int i = 0; i < num; i++)
				if (m.x >= g.node.coor[i].x - 20 && m.x <= g.node.coor[i].x + 20 && m.y >= g.node.coor[i].y - 20 && m.y <= g.node.coor[i].y + 20)
				{
					column = i;
					return true;
				}
			break;
		default:
			break;
		}
	}
	return false;
}


int main() 
{
	initgraph(900, 650);
	setbkcolor(WHITE);
	cleardevice();
	Graph g;
	int Htext = 30;//字体大小
	char s[10];
	setcolor(RED);
	outtextxy(0, 0, "绘图区域");
	rectangle(-10, -10, 600, 500);
	setbkmode(TRANSPARENT);
	settextstyle(Htext, 0, "华文楷体");
	outtextxy(610, 0, "地标个数:");
	Sleep(1000);
	InputBox(s, 10, "请输入数量");
	outtextxy(610,  Htext, s);
	outtextxy(610, Htext * 2, "地标名称:");
	num = atoi(s);
	
	分配内存
	g.node.a = new char* [num];
	for (int j = 0; j < num; j++)
	{
		g.node.a[j] = new char[50];
	}

	g.node.coor = new Coor[num];

	g.edg = new double* [num];
	for (int j = 0; j < num; j++)
	{
		g.edg[j] = new double[num];
	}
	/

	CreateGraph(&g);//初始化

	//输入相关信息
	for (int j = 0; j < num; j++)
	{
		InputBox(g.node.a[j], 10, "请输入地标名");
		outtextxy(610, Htext*(j+3), g.node.a[j]);
	}
	char startdi[50],endi[50];
	outtextxy(0, 510, "起始地:"); InputBox(startdi, 50, "请输入地标名"); outtextxy(textwidth("起始地:")+20, 510, startdi);
	outtextxy(0, 550, "目的地:"); InputBox(endi, 50, "请输入地标名"); outtextxy(textwidth("起始地:") + 20, 550, endi);
	///

	//画点
	setcolor(GREEN);
	bool flag = false; int i = 0; char val;
	MOUSEMSG m;		// 定义鼠标消息
	while (true)
	{
		// 获取一条鼠标消息
		m = GetMouseMsg();
		switch (m.uMsg)
		{
		case WM_LBUTTONDOWN:
			if (flag == false&&m.x<600-20&&m.y<500-20&&m.x>20&&m.y>20) {
				fillcircle(m.x, m.y, 20);
				
				outtextxy(m.x, m.y, g.node.a[i]);
				g.node.coor[i].x = m.x;
				g.node.coor[i].y = m.y;
				i++;
			}
			break;
		case WM_LBUTTONUP:
			if (flag)
				flag = !flag;
			break;
		}
		
		if (i == num)
			break;
	}
	
	//画线
	setcolor(BLUE);
	while (true)
	{
		StartLine(g);
		EndLine(g);
		lineArrow(g);
		sprintf(num_dist, "%d", int(distancess));
		outtextxy((g.node.coor[row].x + g.node.coor[column].x) / 2, (g.node.coor[row].y + g.node.coor[column].y) / 2, num_dist);
		Pan(&g, row, column);
		if (_kbhit())
		{
			val = getch();
			if (val == 27)
				break;
		}	
	}

	//计算最短路径
	Dijkstra(g);
	//输出
	setcolor(RED);
	outtextxy(0, 590, "最短距离为:");
	sprintf(num_dist, "%d", dis(g, startdi, endi));
	outtextxy(textwidth("最短距离为:") + 20, 590, num_dist);

	//释放
	for (int i = 0; i < num; i++)
		delete[]g.node.a[i];
	delete[]g.node.a;
	delete[]g.node.coor;
	for (int i = 0; i < num; i++)
		delete[]g.edg[i];
	delete[]g.edg;
	for (int i = 0; i < num; i++)
		delete[]distances[i];
	delete[]distances;
	/
	_getch();
	closegraph();

	return 0;
}

程序下载

只有源码是不是很枯燥无味呢?现在上传了发布版供大家尝鲜,目前只是实现了这个功能,代码的整理还有待处理
真实截图:

完整运行截图

下载链接:https://gitee.com/lose_recall/MapShortRoad.git
视频链接:https://www.bilibili.com/video/BV1Zo4y1Z7cn

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值