带权有向图的邻接矩阵法表示、单源最短路径——Dijkstra算法、各顶点之间最短路径——Floyd算法

#pragma once
#include 
using namespace std;
#define MaxVertexNum 100
#define INFINITY 999
typedef char VertexType;
typedef int EdgeType;
typedef struct {
     
	VertexType Vertex[MaxVertexNum];
	EdgeType Edge[MaxVertexNum][MaxVertexNum];
	int vertexNum, edgeNum;
}Graph;
bool visited[MaxVertexNum];
bool InitGraph(Graph* g) {
     
	cout << "vertexNum、edgeNum" << endl;
	cin >> g->vertexNum >> g->edgeNum;
	for (int j = 0; j < g->vertexNum; j++) {
     
		g->Vertex[j] = '`';
	}
	for (int i = 0; i < g->vertexNum; i++) {
     
		for (int j = 0; j < g->vertexNum; j++) {
     
			g->Edge[i][j] = INFINITY;
		}
	}
	return true;
}
int GetVertexIndex(Graph* g, VertexType x) {
     
	for (int i = 0; i < g->vertexNum; i++) {
     
		if (g->Vertex[i] == x) {
     
			return i;
		}
	}
	return -1;
}
bool AddEdge(Graph* g, VertexType x, VertexType y,int n) {
     
	int xi = GetVertexIndex(g, x);
	int yi = GetVertexIndex(g, y);
	if (xi >= 0 && yi >= 0) {
     
		g->Edge[xi][yi] = n;
		return true;
	}
	return false;
}
int FirstNeighbor(Graph* g, int v) {
     
	for (int i = 0; i < g->vertexNum; i++) {
     
		if (g->Edge[v][i]!=INFINITY) {
     
			return i;
		}
	}
	return -1;
}
int NextNeighbor(Graph* g, int v, int w) {
     
	for (int i = w + 1; i < g->vertexNum; i++) {
     
		if (g->Edge[v][i] != INFINITY) {
     
			return i;
		}
	}
	return -1;
}
bool CreateGraph(Graph* g) {
     
	cout << "顶点" << endl;
	for (int i = 0; i < g->vertexNum; i++) {
     
		cin >> g->Vertex[i];
	}
	VertexType a, b;
	int n;
	cout << "添加的边" << endl;
	for (int i = 0; i < g->edgeNum; i++) {
     
		cin >> a >> b>>n;
		AddEdge(g, a, b,n);
	}
	return true;
}
void ShortestPath_DIJ(Graph* g, int v0,int v1){
     
	bool* final = new bool[g->vertexNum];
	int* d = new int[g->vertexNum];
	int* path = new int[g->vertexNum];
	for (int i = 0; i < g->vertexNum; i++) {
     
		final[i] = false;
		d[i] = g->Edge[v0][i];
		path[i] = g->Edge[v0][i] == INFINITY ? -1 : 0;	//!!!!!!!!!!!!!!!!!!!!!!!!!
	}
	final[v0] = true;
	d[v0] = 0;
	for (int i = 1; i < g->vertexNum; i++) {
     
		int min = INFINITY;
		int v;
		for (int w = 0; w < g->vertexNum; w++) {
     
			if (!final[w] && d[w] < min) {
     
				min = d[w];
				v = w;
			}
		}
		final[v] = true;
		for (int w = 0; w < g->vertexNum; w++) {
     
			if (!final[w] && d[w]>min+g->Edge[v][w]) {
     
				d[w] = g->Edge[v][w] + min;
				path[w] = v;      //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			}
		}
	}
	cout << path[v1] << endl;
	delete[] d;
	delete[] path;
	delete[] final;
}
void ShortestPath_FLOYD(Graph* g){
     
	int path[5][5];
	for (int j = 0; j < g->vertexNum; j++) {
     
		for (int k = 0; k < g->vertexNum; k++) {
     
			path[j][k] = -1;
		}
	}
	for (int i = 0; i < g->vertexNum; i++) {
     
		for (int j = 0; j < g->vertexNum; j++) {
     
			for (int k = 0; k < g->vertexNum; k++) {
     
				if (g->Edge[j][i] + g->Edge[i][k] < g->Edge[j][k]) {
     
					g->Edge[j][k] = g->Edge[j][i] + g->Edge[i][k];
					path[j][k] = i;
				}
			}
		}
	}
	for (int j = 0; j < g->vertexNum; j++) {
     
		for (int k = 0; k < g->vertexNum; k++) {
     
			cout << path[j][k]<<endl;
		}
		cout << endl;
	}

}
#include "Graph.h"
#include 
int main() {
     
	Graph* g=new Graph;
	InitGraph(g);
	CreateGraph(g);
	ShortestPath_DIJ(g, 0, 1);
	ShortestPath_DIJ(g, 0, 2);
	ShortestPath_DIJ(g, 0, 3);
	ShortestPath_DIJ(g, 0, 4);
	return 0;
}

带权有向图的邻接矩阵法表示、单源最短路径——Dijkstra算法、各顶点之间最短路径——Floyd算法_第1张图片

#include "Graph.h"
#include 
int main() {
     
	Graph* g=new Graph;
	InitGraph(g);
	CreateGraph(g);
	ShortestPath_FLOYD(g);
	return 0;
}

带权有向图的邻接矩阵法表示、单源最短路径——Dijkstra算法、各顶点之间最短路径——Floyd算法_第2张图片

你可能感兴趣的:(c++,图论,tensorflow,算法,java)