之前算法课的作业:
某推销员要从城市 v1 出发,访问其它城市 v2,v3,…,v6 各一次且仅一次,最后返回 v1。D
为各城市间的距离矩阵。问:该推销员应如何选择路线,才能使总的行程最短?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HomeWork1_TSP
{
class Program
{
//对N个数中的M个进行组合,结果保存在一个list中
public static List<List<int>> combine(List<int> array, int n, int m)
{
List<List<int>> result=new List<List<int>>();
List<int> temp = new List<int>(m);
int i = 0;
int j = 0;
bool flag = true;
for (i = 0; i < m; i++)
temp.Add(i);
i = m - 1;
while (true)
{
if (temp[0] == n - m + 1)
break;
if(flag)
{
List<int> tmp=new List<int>();
for (j = 0; j < m; j++)
{
tmp.Add(array[temp[j]]);
}
result.Add(tmp);
flag = false;
}
temp[i]++;

本文探讨了旅行商问题(TSP)的动态规划解决策略,并提供了使用C#编程语言实现的详细步骤。通过这种方法,可以找到使推销员总行程最短的路径。文章覆盖了算法原理及其实现细节。
最低0.47元/天 解锁文章
895

被折叠的 条评论
为什么被折叠?



