hdu 1572(dfs+最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1572
思路:dfs暴搜即可。
View Code
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 #define MAXN 33 6 #define inf 1<<30 7 int map[MAXN][MAXN]; 8 int n,MIN,k; 9 int num[MAXN]; 10 bool mark[MAXN]; 11 12 void dfs(int start,int count,int dist){ 13 if(count==k){ 14 MIN=min(dist,MIN); 15 return ; 16 } 17 for(int i=1;i<=k;i++){ 18 if(!mark[i]){ 19 mark[i]=true; 20 dfs(num[i],count+1,dist+map[start][num[i]]); 21 mark[i]=false; 22 } 23 } 24 } 25 26 27 int main(){ 28 int _case; 29 while(~scanf("%d",&n)&&n){ 30 for(int i=0;i<n;i++){ 31 for(int j=0;j<n;j++){ 32 scanf("%d",&map[i][j]); 33 } 34 } 35 k=0,MIN=inf; 36 scanf("%d",&_case); 37 memset(mark,false,sizeof(mark)); 38 while(_case--){ 39 int x; 40 scanf("%d",&x); 41 if(mark[x])continue; 42 mark[x]=true; 43 num[++k]=x; 44 } 45 memset(mark,false,sizeof(mark)); 46 dfs(0,0,0); 47 printf("%d\n",MIN); 48 } 49 return 0; 50 }