某推销员要从城市v1 出发,访问其它城市v2,v3,…,v6 各一次且仅一次,最后返回v1。D
为各城市间的距离矩阵。
问:该推销员应如何选择路线,才能使总的行程最短?
以下是用动态规划方法,Linux下g++编译通过
#include
<
iostream
>
#include
<
set
>
#include
<
vector
>

#define
MAX 6

using
namespace
std;


int
dis[MAX][MAX]
=
...
{
0, 10, 20, 30, 40, 50,
12, 0 ,18, 30, 25, 21,
23, 19, 0, 5, 10, 15,
34, 32, 4, 0, 8, 16,
45, 27, 11,10, 0, 18,
56, 22, 16,20, 12, 0
}
;

typedef
struct

...
{
int curcity;//当前所在的城市
vector<int> unvisited;//当前未访问的城市
set<int> type;//由于set自动排序,相同状态的vector可能不同,但set必然相同
int distance;//从当前城市到终点回到起点的距离
}
status;


/**/
/*测试用*/
void
printVec( vector
<
status
>
vec)

...
{
vector<status>::iterator iter;
vector<int>::iterator it;
为各城市间的距离矩阵。
问:该推销员应如何选择路线,才能使总的行程最短?
以下是用动态规划方法,Linux下g++编译通过

































