之前算法课的作业:
某推销员要从城市 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]++;