旅行商問題回溯法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/zh-tw/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

發表回復

登錄後才能評論