Bellman-Ford算法的核心就是对边进行松弛操作
贴上c++源代码
#include "stdafx.h"
#pragma warning(disable:4996)
#include <iostream>
using namespace std;
//表示一条边
struct Edge {
int src;
int dest;
int weight;
};
//带有权值的有向图
struct Graph {
int V;
int E;
Edge* edge;
};
//创建图
Graph* CreateGraph(int v, int e) {
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->E = e;
graph->V = v;
graph->edge = (Edge*)malloc(e * sizeof(Edge));
return graph;
}
//打印结果
void Print(int dist[], int n) {
cout << "单元最短路径" << endl;
for (int i = 0; i < n; i++) {
if (dist[i] == INT_MAX) {
cout << "与节点" << i << "距离->无穷大" << endl;
}//if
else {
cout << "与节点" << i << "距离->" << dist[i] << endl;
}
}
}
//单元最短路径
bool BellmanFord(Graph* graph, int src) {
int v = graph->V;
int e = graph->E;
//存储距离
int d