1. 程式人生 > >旅行售貨商問題 1

旅行售貨商問題 1

#include<iostream>  
using namespace std;   
const int INF = 10000000;  
int n, cc = 0, bestc = INF;  
int **g;  
int *x, *bestx;    
void travel(int t) {
    if(t==n){ 
		if(g[x[t-1]][x[t]]!=INF&&g[x[t]][1]!=INF&&(cc+g[x[t-1]][x[t]]+g[x[t]][1]<bestc||bestc==INF)){
			for(int i=0; i<n+1;i++)  
				bestx[i]=x[i];  
				bestc=cc+g[x[t-1]][x[t]]+g[x[t]][1];  
        }  
        return;  
    }  
    for(int i=t;i<n;i++) {  
        if (g[x[t-1]][x[i]]!=INF&&(cc+g[x[t-1]][x[i]]<bestc||bestc==INF)){ 
			//定義巨集swap(x,y)用於交換兩個引數x和y的值,並編寫程式測試。 
			swap(x[i],x[t]);            
			cc+=g[x[t-1]][x[t]];  
			travel(t+1);  
			cc-=g[x[t-1]][x[t]];  
			swap(x[i],x[t]);  
        }  
    }  
}   

void output(){  
	printf("輸出最小路徑:\n");
	printf("%d\n",bestc);
	printf("輸出最優路線:\n");
	printf("%d",bestx[1]);
    for (int i=2;i<n+1;i++)  	
		printf(" %d",bestx[i]);
	printf(" %d\n",bestx[1]);
}  
  
int main(){  
    n = 4;  
    g = new int*[n+1];  
    x = new int[n+1];  
    bestx = new int[n+1];   
    for (int i=0;i<n +1;i++) {  
        g[i] = new int[n+1];  
        x[i] = i;  
        for (int j=0; j<n+1;j++)  
            g[i][j]=INF;  
    }  
    g[1][2] = g[2][1] = 30;  
    g[1][3] = g[3][1] = 6;  
    g[1][4] = g[4][1] = 4;   
    g[2][3] = g[3][2] = 5;  
    g[2][4] = g[4][2] = 10;  
    g[3][4] = g[4][3] = 20;  
    travel(2);  
    output();  
    return 0;  
}