Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C++ by 20cm ( 8 years ago )
#include<iostream>using namespace std;
int n, C[100][100], A[100];
int XOPT[100], FOPT;
bool chuaxet[100];
bool changed = false; //Kiem tra xem FOPT co thay doi khong, neu khong se cho biet la k co ket qua
void Init(){
cout << "Nhap n: "; cin >>n;
for(int i=1; i<=n; i++) chuaxet[i]=true;
cout << "Nhap ma tran:" << endl i=1; i<=n; j=1; j<=n;>>C[i][j];
cout << "Nhap gioi han: ";
cin >>FOPT;
}
int totalCost(){
int cost = 0;
for(int i=1; i<=n; i++)
cost += C[A[i-1]][A[i]];
cost += C[A[n]][A[1]];
return cost;
}
bool Check_min(){
if(totalCost() < FOPT)
return true;
else return false;
}
void Update_result(){
changed = true; //
FOPT = totalCost();
for(int i=1; i<=n; i++)
XOPT[i] = A[i];
XOPT[n+1] = A[1];
}
void Try(int i){
for(int j=1; j<=n; j++){
if(chuaxet[j]==true){
A[i]=j; chuaxet[j]=false;
if(i==n){
if(Check_min() == true)
Update_result();
}
else Try(i+1);
chuaxet[j]=true;
}
}
}
void Result(){
if(changed == true){
cout << "Ket qua toi uu: " << FOPT << endl;
cout << "Phuong an toi uu: ";
for(int i=1; i<=n+1; i++)
cout << XOPT[i] << " ";
}
else cout << "Gioi han qua be!";
}
int main(){
Init();
Try(1);
Result();
return 0;
}
Revise this Paste