Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1572
Well, I didn't think it could be done with a deep search. Also thought is a set of Dijkstra algorithm template problem.
#include <iostream> #include <string> #include <cstdio> #include <cstring> #include <map> #include <queue> #i nclude<cmath> #include <stack> #include <set> #include <vector> #include <algorithm># Define LL long Long#define inf 1<<29#define s (a) scanf ("%d", &a) #define CL (A, B) memset (A,b,sizeof (a)) using namespace Std;const int n=35;int n,m,a,b[n],c;string s1,s2;int map[n][n];int dist,num,ans;bool vis[N];/* deep Search, searching for every possible situation, Mark, deep layer, backtracking. */void dfs (int x,int step,int dist) {if (step==num) if (ans>dist) ans=dist; for (int i=1;i<n;i++) if (B[i]) {b[i]=0; DFS (I,step+1,dist+map[x][i]); B[i]=1; }}int Main () {while (~SCANF ("%d", &n)} {if (n<=0) break; CL (b,0); for (int. i=0;i<n;i++) for (int j=0;j<n;j++) s (Map[i][j]); S (m); num=0; for (int i=1;i<=m;i++) {s (a); if (!b[a]) {b[a]=1; num++; }} Ans=inf; DFS (0,0,0); printf ("%d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu1572-(2)-Shortest way (deep search)