Professional Documents
Culture Documents
TSP Sol
TSP Sol
int G[maxn][maxn];
bool C[maxn]={false}, cn = false;
int X[maxn],Y[maxn], n, m, ans=inf;
int t=0;
for (int i=2;i<=n;i++){
t+=G[X[i-1]][X[i]];
}
t+=G[X[n]][X[1]];
//Nếu nghiệm này tốt hơn thì ghi nhận
//và lưu vector nghiệm X vào Y
if (t<ans){
for (int i=1;i<=n;i++) Y[i] = X[i];
ans = t;
cn=true;
}
}
Try(1);
if (cn){
cout << ans << endl;
for (int i=1; i<=n; i++) cout << Y[i] << " ";
cout << Y[1];
} else cout << -1;