栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

旅行商问题【回溯】(C语言)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

旅行商问题【回溯】(C语言)

旅行商问题【回溯】(C语言)

给定一系列城市和每对城市之间的距离,求解访问每一座城市一次并回到起始城市的最短回路。


可以看出abcdea这条线路是最短的。
我们把abcde抽象为12345进行算法设计。有两种思路:固定起点和不固定起点。

固定起点

代码

#include 
#include 
#define MAXSIZE 10
#define FINITY 1000
int thingnum; //节点数
int bian;//边个数
int a[MAXSIZE][MAXSIZE];//邻接矩阵
int x[MAXSIZE]={0,3,2,5,1,4};


int cc=0;
int bestc=FINITY;
int bestx[MAXSIZE];
//输出邻接矩阵
void outputjuzhen()
{
    int i,j;
    for(i=1;i<=thingnum;i++)
    {
        for(j=1;j<=thingnum;j++)
        {
            printf("%-5d",a[i][j]);
        }
        printf("n");
    }
}
//交换
void swap(int t,int i)
{
    int temp;
    temp = x[t];
    x[t] = x[i];
    x[i] = temp;
}

void bactrack(int i)
{
    int j,k;
    if(i==thingnum)
    {
        if(a[x[thingnum-1]][x[thingnum]]!=FINITY && a[x[thingnum]][x[1]]!=FINITY && (cc+a[x[thingnum-1]][x[thingnum]]+a[x[thingnum]][x[1]] < bestc || bestc ==FINITY))
        {
            for(j=1;j<=thingnum;j++)
            {
                bestx[j] = x[j];
            }
            bestc = cc+a[x[thingnum-1]][x[thingnum]]+a[x[thingnum]][x[1]];

        }
    }
    else
    {
        for(int j=i;j<=thingnum;j++)
        {
            if(a[x[i-1]][x[j]]!=FINITY && (cc+a[x[i-1]][x[j]] 

不固定起点

代码

#include 
#include 
#define MAXSIZE 10
#define FINITY 1000
int thingnum; //节点数
int bian;//边个数
int a[MAXSIZE][MAXSIZE];//邻接矩阵
int x[MAXSIZE]={0,3,2,5,1,4};


int cc=0;
int bestc=FINITY;
int bestx[MAXSIZE];
//输出邻接矩阵
void outputjuzhen()
{
    int i,j;
    for(i=1;i<=thingnum;i++)
    {
        for(j=1;j<=thingnum;j++)
        {
            printf("%-5d",a[i][j]);
        }
        printf("n");
    }
}
//交换
void swap(int t,int i)
{
    int temp;
    temp = x[t];
    x[t] = x[i];
    x[i] = temp;
}

void bactrack(int i)
{
    int j,k;
    if(i==thingnum)
    {
        if(a[x[thingnum-1]][x[thingnum]]!=FINITY && a[x[thingnum]][x[1]]!=FINITY && (cc+a[x[thingnum-1]][x[thingnum]]+a[x[thingnum]][x[1]] <= bestc || bestc ==FINITY))
        {
            for(j=1;j<=thingnum;j++)
            {
                bestx[j] = x[j];
            }
            //printf("na[x[thingnum-1]][x[thingnum]]:%d",a[x[thingnum-1]][x[thingnum]]);
            //printf("na[x[thingnum]][1]:%d",a[x[thingnum]][x[1]]);
            bestc = cc+a[x[thingnum-1]][x[thingnum]]+a[x[thingnum]][x[1]];
            printf("nbestx[MAXSIZE]:");
            for(i=1;i<=thingnum;i++)
            {
                printf("%-5d",bestx[i]);
            }
            printf("%-5d",bestx[1]);
            printf("nbestc:%d",bestc);

        }
    }
    else
    {
        for(int j=i;j<=thingnum;j++)
        {
            if(i==1)
            {
                swap(i,j);
                bactrack(i+1);
                swap(i,j);
            }
            else
            {
                if(a[x[i-1]][x[j]]!=FINITY && (cc+a[x[i-1]][x[j]] 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/648514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号