exam8-4.cpp

来自「包含各种测试,查找和算法等代码,如冒泡算法,树的遍历,链表,队列,堆栈等」· C++ 代码 · 共 40 行

CPP
40
字号
#include <iostream.h>
#include <stdlib.h>

typedef char VerT;
typedef char DataType;
const int MaxVertices = 100;		
const int MaxWeight = 9999;	
	
#include "AdjMWGraph.h"
#include "Dijkstra.h"
#include "CreatAdjMWGraph.h"

void main(void)
{
	AdjMWGraph g;
	char a[] = {'A','B','C','D','E','F'};
	RowColWeight rcw[] = {{0,2,5},{0,3,30},{1,0,2},{1,4,8},{2,1,15},{2,5,7},
		{4,3,4},{5,3,10},{5,4,18}};
	int n = 6, e = 9;

	CreatGraph(g, a, n, rcw, e);

	int *distance = new int[n];
	int *path = new int[n];

	Dijkstra(g, 0, distance, path);

	cout << "从顶点A到其它各顶点的最短距离为:" << endl;
	for(int i = 1; i < n; i++)
		cout << "到顶点 " << g.GetValue(i) 
			<< " 的最短距离为:" << distance[i] << endl;

	cout << "从顶点A到其它各顶点的前一顶点分别为:" << endl;
	for(i = 0; i < n; i++)
		if(path[i] != -1)
			cout << "到顶点 " << g.GetValue(i) 
				<< " 的前一顶点为 " << g.GetValue(path[i]) << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?