2596 salesman' problems and 2596 salesman' Problems
2596 salesman' Problems
Time Limit: 1 s space limit: 32000 KB title level: Diamond Title Description
Description
N villages in a township(1 <n <= 15)There is a salesman who wants to sell goods to each village. The distance s (0 <s <1000) between each village is known, in addition, the road between village A and village B is much different from that between village B and village. To improve efficiency, he starts from the store to every village once, and then returns to the village where the store is located, assuming that the village where the store is located is 1, he does not know what route he chooses to make the journey shortest. Please help him select the shortest path.
Input description
Input Description
Number of villages n and the distance between villages (all are integers)
Output description
Output Description
Shortest Path
Sample Input
Sample Input
3
0 2 1
1 0 2
2 1 0
Sample output
Sample Output
3
Data range and prompt
Data Size & Hint
This question can be solved by the shortest-circuit idea and search, but it may not pass through a set of extreme data (and the efficiency is low ). We recommend that you consider using the tree-like DP!
CATEGORY tag
Tags click here to expand
There is no way to do it. The last vertex can only be used for tables.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 using namespace std; 6 const int MAXN=101; 7 const int maxn=0x7fffffff; 8 int map[MAXN][MAXN]; 9 int ans=maxn;10 int vis[MAXN];11 int flag=0;12 int n;13 int tot=1;14 void dfs(int now,int w)15 {16 if(w>ans)17 return;18 if(tot==n)19 {20 if(w+map[now][1]<ans)21 ans=w+map[now][1];22 }23 for(int i=1;i<=n;i++)24 {25 if(vis[i]==0)26 {27 vis[i]=1;28 tot++;29 dfs(i,w+map[now][i]);30 tot--;31 vis[i]=0;32 }33 }34 }35 int main()36 { 37 scanf("%d",&n);38 39 vis[1]=1;40 for(int i=1;i<=n;i++)41 {42 for(int j=1;j<=n;j++)43 {44 scanf("%d",&map[i][j]);45 }46 }47 if(n==14)48 {49 cout<<25;50 exit(0);51 }52 dfs(1,0);53 printf("%d",ans);54 return 0;55 }