旅行商问题回溯法c语言,旅行商问题回溯算法伪代码

本文目录一览:

求大神改一下这个代码 回溯法的任务分配问题 用C语言

#include stdio.h

#include string.h

int n, a[21][21], sum, best, b[21], c[21], d[21];

void f(int m) {

int i,j;

if(mn) {

if(best==-1||sumbest)

best=sum;

memcpy(d,c,sizeof(d));

return;

}

for(j=1;j=n;j++) {

if(!b[j]) {

sum+=a[m][j]; b[j]=1;

c[m] = j;

if(sumbest||best==-1)

f(m+1);

sum-=a[m][j]; b[j]=0;

}

}

}

int main() {

int i,j;

printf(“请输入作业数:”);

scanf(“%d”,n);

printf(“作业分配给不同工人所需费用: “);

for(i=1;i=n;i++)

for(j=1;j=n;j++)

scanf(“%d”,a[i][j]);

sum=0;

best=-1;

f(1);

printf(“最小总费用为:%d\n”, best);

printf(“作业”);

for(i=1;i=n;i++) {

printf(“%3d”, i);

}

printf(“分别分给第”);

for(i=1;i=n;i++) {

printf(“%3d”, d[i]);

}

puts(“个工人!作业分配完毕!”);

return 0;

}

用动态规划做旅行商问题 用C语言 各位大虾帮帮忙啊

#include list

#include iostream

using namespace std ;

typedef listint 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)

{

// cout”——————-“n”-start——-“endl;

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;

}

//cout”—“*i”-“”1″”=”minValueendl;

}

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;

}

/*

cout”—-“temp”–“*(i)”=”minValue”–链表里还剩有如下元素”;

for (i = list_org.begin(); i != list_org.end(); ++i)

cout *i ” “;

cout endl;

*/

for(int j=2;jn;j++)

{

i=list_org.begin();

for(int k=1;kj;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);

/*

cout”—-“”—“temp”–“*(i)”=”tempvalue”–所有的”;

for (i = list_org.begin(); i != list_org.end(); ++i)

cout *i ” “;

cout endl;

*/

if(tempvalueminValue){

if(list_org.size()==matrix_length-1){

list_result=list_org;

}

minValue=tempvalue;

}

}

}

//cout”——————-“n”—end—–“endl;

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”旅行商问题的动态规划算法”endl;

cout”路线长度的矩阵表示如下,-1表示无限大”endl;

for(int j=0;jmatrix_length;j++){

coutendl;

for(int k=0;kmatrix_length;k++){

cout” “d[j][k];

}

}

coutendl;

cout”计算结果:”getPath(4,list_org)endl;

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;

cini;

return 0;

}

已知12个地点间的距离,每个地点都要去一次,最后回到起点。求最短距离。TSP旅行商问题。

这个是NP完全问题,只能求近似解,目前没有简单又高效的方法,最简单的就是穷举算法和最邻近算法了,其它的可以考虑插入算法,回溯法,遗传算法,蚁群算法,模拟退火算法等。。。

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

旅行商问题(Traveling Saleman Problem,TSP)又译为旅行推销员问题、货郎担问题,简称为TSP问题,是最基本的路线问题,该问题是在寻求单一旅行者由起点出发,通过所有给定的需求点之后,最后再回到原点的最小路径成本。字面上的理解是:有一个推销员,要到n个城市推销商品,他要找出一个包含所有n个城市的具有最短路程的环路。

解决TSP问题的思想有回溯法、贪心法、动态规划法等。

如果动态规划法解决TSP问题,可以参考程序代码:

#include list

#include iostream

using namespace std ;

typedef listint 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;jn;j++)

{

i=list_org.begin();

for(int k=1;kj;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(tempvalueminValue)

{

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”旅行商问题动态规划算法”endl;

cout”路线长度的矩阵表示如下 (-1表示无限大)”endl;

for(int j=0;jmatrix_length;j++){

coutendl;

for(int k=0;kmatrix_length;k++){

cout” “d[j][k];

}

}

coutendl;

cout”计算结果:”getPath(4,list_org)endl;

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;

cini;

return 0;

}

可运行的c语言程序:旅行商求最短路径问题

在无向完全图中,对于任意两个顶点vi和vj,我们可以在多项式时间内找到vi和vj这两个顶点之间的所有路径,选择其中路程最短的一条,令S[i,j]表示vi和vj这两个顶点之间最短距离的那条路径。搜索路径S[i,j],找到vi到达的在S[i,j]上的第一个顶点,记该顶点为vk,将其记录在数组中R[][],递归查找vi到vk和vk到vj的最短路径及其相应权值,最后将数组D[]中的顶点和权值之和打印出来即为所求,并用画图函数将行经过程画出。

求大神给这个程序做个详细的注释 C语言 回溯法 任务分配问题

这个题目分解一下,就是将n个数据排列组合,数学算法可以得到种数为A(n,n)=n!

然后在这n!种可能种找到花费最少的那一种就行了。

以下是我写的程序,验证了一下,好像没有什么问题,你看看。

#include stdio.h

int best,last;

int b[20],c[20],d[20],a[20][20];

void get_best(int n)

{

int j,k;

last = 0;

for(j=0;jn;j++)

last += a[j][c[j]];

if(best == 0)

{

best = last;

for(k=0; kn; k++)

d[k] = c[k];

}

else

{

if(last  best)

{

best = last;

for(k=0; kn; k++)

d[k] = c[k];

}

}

}

void f(int n,int count) 

{

int i,t,j;

if(n == 1)

{

c[count] = b[0];

get_best(count+1);

}

else

{

for(i=0; in; i++)

{

t = b[i];

b[i] = b[n-1];

b[n-1] = t;

c[count] = t;

f(n-1,count+1);

t = b[i];

b[i] = b[n-1];

b[n-1] = t;

}

}

}

int main() 

{

    int i,j;

int n;

    printf(“请输入作业数:”);

    scanf(“%d”,n);

    printf(“作业分配给不同工人所需费用:\n”);

    for(i=0;in;i++)

{

b[i] = i;

c[i] = -1;

d[i] = -1;

        for(j=0;jn;j++)

            scanf(“%d”,a[i][j]);

}

best = last = 0;

f(n,0);

printf(“最小总费用为:%d\n”, best);

for(i=0;in;i++)

{

printf(“第%d人分:”,i+1);

printf(“第%d工作. “, d[i]+1);

printf(“金钱为:%d\n”,a[i][d[i]]);

}

puts(“\n”);

return 0;

}

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/244638.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝的头像小蓝
上一篇 2024-12-12 13:03
下一篇 2024-12-12 13:03

相关推荐

  • Python官网中文版:解决你的编程问题

    Python是一种高级编程语言,它可以用于Web开发、科学计算、人工智能等领域。Python官网中文版提供了全面的资源和教程,可以帮助你入门学习和进一步提高编程技能。 一、Pyth…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • 如何解决WPS保存提示会导致宏不可用的问题

    如果您使用过WPS,可能会碰到在保存的时候提示“文件中含有宏,保存将导致宏不可用”的问题。这个问题是因为WPS在默认情况下不允许保存带有宏的文件,为了解决这个问题,本篇文章将从多个…

    编程 2025-04-29
  • Python实现爬楼梯算法

    本文介绍使用Python实现爬楼梯算法,该算法用于计算一个人爬n级楼梯有多少种不同的方法。 有一楼梯,小明可以一次走一步、两步或三步。请问小明爬上第 n 级楼梯有多少种不同的爬楼梯…

    编程 2025-04-29
  • AES加密解密算法的C语言实现

    AES(Advanced Encryption Standard)是一种对称加密算法,可用于对数据进行加密和解密。在本篇文章中,我们将介绍C语言中如何实现AES算法,并对实现过程进…

    编程 2025-04-29
  • 学习Python对学习C语言有帮助吗?

    Python和C语言是两种非常受欢迎的编程语言,在程序开发中都扮演着非常重要的角色。那么,学习Python对学习C语言有帮助吗?答案是肯定的。在本文中,我们将从多个角度探讨Pyth…

    编程 2025-04-29
  • Harris角点检测算法原理与实现

    本文将从多个方面对Harris角点检测算法进行详细的阐述,包括算法原理、实现步骤、代码实现等。 一、Harris角点检测算法原理 Harris角点检测算法是一种经典的计算机视觉算法…

    编程 2025-04-29
  • 数据结构与算法基础青岛大学PPT解析

    本文将从多个方面对数据结构与算法基础青岛大学PPT进行详细的阐述,包括数据类型、集合类型、排序算法、字符串匹配和动态规划等内容。通过对这些内容的解析,读者可以更好地了解数据结构与算…

    编程 2025-04-29
  • Python被称为胶水语言

    Python作为一种跨平台的解释性高级语言,最大的特点是被称为”胶水语言”。 一、简单易学 Python的语法简单易学,更加人性化,这使得它成为了初学者的入…

    编程 2025-04-29
  • Java Thread.start() 执行几次的相关问题

    Java多线程编程作为Java开发中的重要内容,自然会有很多相关问题。在本篇文章中,我们将以Java Thread.start() 执行几次为中心,为您介绍这方面的问题及其解决方案…

    编程 2025-04-29

发表回复

登录后才能评论