-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-2.cpp
More file actions
59 lines (59 loc) · 1.22 KB
/
Copy path5-2.cpp
File metadata and controls
59 lines (59 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<algorithm>
#define MAX 100
using namespace std;
int n;
int a[MAX][MAX];
int x[MAX];
int bestx[MAX] = {0};
int bestp = 63355;
int cp = 0;
void backpack(int t){
if(t>n){
if((a[x[n]][1])&&(a[x[n]][1]+cp<bestp)){
bestp = a[x[n]][1]+cp;
for(int i = 1;i<=n;i++){
bestx[i] = x[i];
}
}
}else{
for(int i = t;i<=n;i++){
if((a[x[t-1]][x[i]])&&(cp+a[x[t-1]][x[i]]<bestp)){
swap(x[t],x[i]);
cp+=a[x[t-1]][x[t]];
backpack(t+1);
cp-=a[x[t-1]][x[t]];
swap(x[t],x[i]);
}
}
}
}
int main(){
cout<<"输入城市个数:"<<endl;
cin>>n;
for(int i = 1;i<=n;i++){
x[i] = i;
}
cout<<"输入城市之间的距离(0表示城市间不通):"<<endl;
for(int i = 1;i<=n;i++){
for(int j = 1;j<=n;j++){
cin>>a[i][j];
}
}
backpack(2);
cout<<"最少旅行费用为: "<<bestp<<endl;
cout<<"旅行路径为:"<<endl;
for(int i = 1;i<=n;i++){
cout<<bestx[i]<<" ";
}
cout<<bestx[1];
return 0;
}
/*
input:
4
0 30 6 4
30 0 5 10
6 5 0 20
4 10 20 0
*/