首页 > 编程知识 正文

旅行商问题的应用,旅行商问题时间复杂度

时间:2023-05-06 12:46:26 阅读:153400 作者:1937

# include iostream # include algorithm # include cmath # include cstdio # includecstringusingnamespacestd; const int maxx=105; const int inf=0x3f3f3f3f; int g[maxx][maxx]; int x[maxx]; int bestx[maxx]; int bestl; int cl; int n,m; void init () memset (bestx,0,sizeof ) bestx ); memset(g,0,sizeof(g ) g ); memset(x,0,sizeof(x ) x ); cl=0; bestl=inf; for(intI=1; i=n; I ) {x[i]=i; bestx[i]=i; }for(intI=1; i=n; I ) for(intj=1; j=n; j () if ) I==j ) ) {g[i][j]=0; }else{g[i][j]=inf; }}}voidtraveling(intt ) if(TN ) /到达叶节点) /销售货物的最后一个城市与居住地城市相连,且路径长度小于当前的最佳值,//表明找到了更好的路径。 i=n; I ) {bestx[i]=x[i]; }bestl=cl g[x[n]][1]; }}else{//未到达叶节点for (intj=t; j=n; j ) (/搜索扩展节点的所有分支(如果t-1城市与第t个城市有边界,并且可能获得更短的根if (g [ x [ t-1 ] ) x[j]!=INF(clg[x[t-1](x[j] ) bestL ) ) /将第t个要去的城市号保存在x[t]中,然后前进到t 1层swap(x[t],x[j] );//两个元素的值cl=g[x[t-1]][x[t]]; traveling(t1; //t从第一层扩展节点继续搜索第一层//t 1层,然后搜索第一层cl-=g[x[t-1]][x[t]]; swap(x[t],x[j]; }}}}int main () while ) scanf ) ' %d%d ),n,m )!=EOF () {init; for(intI=1; i=m; I ) {int a,b,cost; scanf () %d%d )、a、b、cost ); g[a][b]=g[b][a]=cost; }traveling(2; cout '最短距离: 'bestlendl; cout '最短路径: '; for(intI=1; i=n; I ) {coutbestx[i] '; }coutendl; }return 0; (/* 59121015121442554624853302315437 ) /

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。