add
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#define MAX_CITIES 4 // 设定城市数量为4
|
||||
#define INF INT_MAX
|
||||
int n = MAX_CITIES; // 城市数量
|
||||
int dist[MAX_CITIES][MAX_CITIES] = { // 距离矩阵
|
||||
{0, 30, 6, 4}, // 城市 0 到其他城市的距离
|
||||
{30, 0, 5, 10}, // 城市 1 到其他城市的距离
|
||||
{6, 5, 0, 20}, // 城市 2 到其他城市的距离
|
||||
{4, 10, 20, 0} // 城市 3 到其他城市的距离
|
||||
};
|
||||
int bestPath[MAX_CITIES]; // 最优路径
|
||||
int bestCost = INF; // 最优路径的总成本
|
||||
// 计算从节点current到下一个节点的界限
|
||||
int calculateLowerBound(int path[], int current) {
|
||||
int lowerBound = 0;
|
||||
int visited[MAX_CITIES] = {0};
|
||||
// 计算已访问的路径的距离
|
||||
for (int i = 0; i < current; i++) {
|
||||
lowerBound += dist[path[i]][path[i + 1]];
|
||||
visited[path[i]] = 1;
|
||||
}
|
||||
// 计算当前节点到其他未访问节点的最短距离
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (!visited[i]) {
|
||||
int minDist = INF;
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (!visited[j] && dist[i][j] < minDist) {
|
||||
minDist = dist[i][j];
|
||||
}
|
||||
}
|
||||
lowerBound += minDist;
|
||||
}
|
||||
}
|
||||
return lowerBound;
|
||||
}
|
||||
// 分支限界法求解TSP
|
||||
void branchAndBound(int path[], int current, int cost) {
|
||||
if (current == n) {
|
||||
// 所有城市都已访问,检查是否是最优解
|
||||
cost += dist[path[current - 1]][path[0]]; // 加上返回起点的距离
|
||||
if (cost < bestCost) {
|
||||
bestCost = cost;
|
||||
for (int i = 0; i < n; i++) {
|
||||
bestPath[i] = path[i];
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
// 计算当前路径的界限
|
||||
int lowerBound = cost + calculateLowerBound(path, current);
|
||||
// 如果当前解已经不可能优于最优解,剪枝
|
||||
if (lowerBound >= bestCost) return;
|
||||
// 继续扩展路径
|
||||
for (int i = 0; i < n; i++) {
|
||||
int flag = 0;
|
||||
for (int j = 0; j < current; j++) {
|
||||
if (path[j] == i) {
|
||||
flag = 1; // 检查当前城市是否已访问
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag == 0) {
|
||||
path[current] = i;
|
||||
branchAndBound(path, current + 1, cost + dist[path[current - 1]][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
int path[MAX_CITIES];
|
||||
path[0] = 0; // 从第一个城市出发
|
||||
branchAndBound(path, 1, 0);
|
||||
printf("\n最优路径是: ");
|
||||
for (int i = 0; i < n; i++)
|
||||
printf("%d ", bestPath[i]);
|
||||
printf("\n总路程: %d\n", bestCost);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#define MAX_CITIES 4 // 城市数量
|
||||
#define INF INT_MAX
|
||||
int n = MAX_CITIES; // 城市数量
|
||||
int dist[MAX_CITIES][MAX_CITIES] = {
|
||||
{0, 30, 6, 4}, {30, 0, 5, 10},
|
||||
{6, 5, 0, 20}, {4, 10, 20, 0}
|
||||
};
|
||||
int visited[MAX_CITIES]; // 标记城市是否访问
|
||||
int bestPath[MAX_CITIES]; // 最优路径
|
||||
int bestCost = INF; // 最优路径的总成本
|
||||
// 回溯法求解TSP
|
||||
void backtrack(int currentCity, int count, int cost, int path[]) {
|
||||
if (count == n) { // 所有城市都已访问,检查是否是最优解
|
||||
cost += dist[currentCity][0]; // 加上返回起点的距离
|
||||
if (cost < bestCost) {
|
||||
bestCost = cost;
|
||||
for (int i = 0; i < n; i++)
|
||||
bestPath[i] = path[i];
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {// 尝试所有未访问的城市
|
||||
if (!visited[i]) { // 访问城市i
|
||||
visited[i] = 1; path[count] = i;
|
||||
// 递归访问下一个城市
|
||||
backtrack(i, count + 1, cost + dist[currentCity][i], path);
|
||||
visited[i] = 0; // 回溯
|
||||
}
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
int path[MAX_CITIES];
|
||||
visited[0] = 1; // 从第一个城市出发
|
||||
path[0] = 0; // 起点是城市0
|
||||
backtrack(0, 1, 0, path); // 从城市0开始回溯
|
||||
printf("\n最优路径是: ");
|
||||
for (int i = 0; i < n; i++)
|
||||
printf("%d ", bestPath[i]);
|
||||
printf("\n最小路程: %d\n", bestCost);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user