永发信息网

什么是商旅问题啊?用c语言设计,是关于图的程序。最好能给出代码

答案:2  悬赏:0  手机版
解决时间 2021-03-14 03:22
什么是商旅问题啊?用c语言设计,是关于图的程序。最好能给出代码
最佳答案
旅行商问题(Traveling Saleman Problem,TSP)又译为旅行推销员问题、货郎担问题,简称为TSP问题,是最基本的路线问题,该问题是在寻求单一旅行者由起点出发,通过所有给定的需求点之后,最后再回到原点的最小路径成本。字面上的理解是:有一个推销员,要到n个城市推销商品,他要找出一个包含所有n个城市的具有最短路程的环路。
解决TSP问题的思想有回溯法、贪心法、动态规划法等。

如果动态规划法解决TSP问题,可以参考程序代码:
#include
#include
using namespace std ;

typedef list LISTINT;

LISTINT listAnother;
LISTINT list_result;

int d[4][4]={{-1,3,6,7},{2,-1,8,6},{7,3,-1,5,},{7,3,7,-1}};
int matrix_length=4;

int getPath(int n,LISTINT list_org)
{

LISTINT::iterator i;

int minValue;
if(n==1)
{
i=list_org.begin();
minValue= d[*i-1][0];
if(list_org.size()==matrix_length-1)
{
list_result=list_org;
}
}
else
{
int temp;
i=list_org.begin();
temp=*i;
list_org.erase(i);
i=list_org.begin();
minValue=d[temp-1][*(i)-1]+getPath(n-1,list_org);
if(list_org.size()==matrix_length-1)
{
list_result=list_org;
}

for(int j=2;j {
i=list_org.begin();
for(int k=1;k {
i++;
}

int tempvalue=*i;
list_org.erase(i);
list_org.push_front(tempvalue);
i=list_org.begin();
tempvalue=d[temp-1][*(i)-1]+getPath(n-1,list_org);

if(tempvalue {
if(list_org.size()==matrix_length-1)
{
list_result=list_org;
}
minValue=tempvalue;
}

}
}
return minValue;
}
int main(int argc, char* argv[])
{

LISTINT list_org;
LISTINT::iterator h;
list_org.push_front(4);
list_org.push_front(3);
list_org.push_front(2);
list_org.push_front(1);
cout<<"旅行商问题动态规划算法"< cout<<"路线长度的矩阵表示如下 (-1表示无限大)"< for(int j=0;j cout< for(int k=0;k cout<<" "< }
}
cout<
cout<<"计算结果:"< list_result.push_front(1);
list_result.push_back(1);
cout<<"要走的路径:---->:";
for (h = list_result.begin(); h != list_result.end(); ++h)

cout << *h << " ";

cout << endl;
int i;
cin>>i;
return 0;
}
全部回答
旅行商问题(traveling saleman problem,tsp)又译为旅行推销员问题、货郎担问题,简称为tsp问题,是最基本的路线问题,该问题是在寻求单一旅行者由起点出发,通过所有给定的需求点之后,最后再回到原点的最小路径成本。字面上的理解是:有一个推销员,要到n个城市推销商品,他要找出一个包含所有n个城市的具有最短路程的环路。 解决tsp问题的思想有回溯法、贪心法、动态规划法等。 如果动态规划法解决tsp问题,可以参考程序代码: #include &lt;list&gt; #include &lt;iostream&gt; using namespace std ; typedef list&lt;int&gt; listint; listint listanother; listint list_result; int d[4][4]={{-1,3,6,7},{2,-1,8,6},{7,3,-1,5,},{7,3,7,-1}}; int matrix_length=4; int getpath(int n,listint list_org) { listint::iterator i; int minvalue; if(n==1) { i=list_org.begin(); minvalue= d[*i-1][0]; if(list_org.size()==matrix_length-1) { list_result=list_org; } } else { int temp; i=list_org.begin(); temp=*i; list_org.erase(i); i=list_org.begin(); minvalue=d[temp-1][*(i)-1]+getpath(n-1,list_org); if(list_org.size()==matrix_length-1) { list_result=list_org; } for(int j=2;j&lt;n;j++) { i=list_org.begin(); for(int k=1;k&lt;j;k++) { i++; } int tempvalue=*i; list_org.erase(i); list_org.push_front(tempvalue); i=list_org.begin(); tempvalue=d[temp-1][*(i)-1]+getpath(n-1,list_org); if(tempvalue&lt;minvalue) { if(list_org.size()==matrix_length-1) { list_result=list_org; } minvalue=tempvalue; } } } return minvalue; } int main(int argc, char* argv[]) { listint list_org; listint::iterator h; list_org.push_front(4); list_org.push_front(3); list_org.push_front(2); list_org.push_front(1); cout&lt;&lt;"旅行商问题动态规划算法"&lt;&lt;endl; cout&lt;&lt;"路线长度的矩阵表示如下 (-1表示无限大)"&lt;&lt;endl; for(int j=0;j&lt;matrix_length;j++){ cout&lt;&lt;endl; for(int k=0;k&lt;matrix_length;k++){ cout&lt;&lt;" "&lt;&lt;d[j][k]; } } cout&lt;&lt;endl; cout&lt;&lt;"计算结果:"&lt;&lt;getpath(4,list_org)&lt;&lt;endl; list_result.push_front(1); list_result.push_back(1); cout&lt;&lt;"要走的路径:----&gt;:"; for (h = list_result.begin(); h != list_result.end(); ++h) cout &lt;&lt; *h &lt;&lt; " "; cout &lt;&lt; endl; int i; cin&gt;&gt;i; return 0; }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
金秀贤递水给全智贤喝为什么要撕掉包装纸
在户籍当地乡镇上办一家广告店收益怎么样?
云阳休闲中心这个地址在什么地方,我要处理点
一根光纤可以承载多大的网速?
大毛餐馆怎么去啊,有知道地址的么
我在重庆想买一些火锅底料 大约一箱左右 秋霞
韩国伊思红参蜗牛霜韩国卖多少钱
人头马ysop价格那么便宜是怎么回事情
安信证券里面的交割单的成交金额有没有包括全
温州鱼圆柳市店地址在什么地方,想过去办事
庐山的“四大奇谜”有什么?
圣经都是神的话语吗
长安4500前平衡杆断了对车有什么影响
南宁新秀区竹溪大道离那个高铁站近
哈尔滨北方国际青年旅舍这个地址在什么地方,
推荐资讯
小学生用文言文写的作文
概述小霸王醉入销金帐 花和尚大闹桃花村这回
进入系统后时间被更改,。进入bios时间是设定
小狗拉出来细白条的东西,是怎么回事啊?
《小混混与乖乖女的爱情》TXT全集下载
魔兽世界单机版3.22 我刚刚把游戏的启动数据
一词多义“使”有哪些意思
从河北沧州邮快递几天能到定州
在这里有个未完的梦歌词是什么歌
大家对女流有什么看法
禹里镇农村社区综合服务社这个地址在什么地方
纪晓岚对联故事
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?