好久没写搜索的题目了 复习一下/。/ 这道题目是暴力的全排列
#include<cstdio>
#include<iostream>
#include<cstring>
#define maxn 31
#define inf 1<<29
using namespace std;
int mapp[maxn][maxn],line[8],minn,vis[8];
int k;
int mymin(int x,int y)
{
if(x>y) return y;
else return x;
}
void dfs(int pos,int time,int cost)
{
if(time>k)
{
//cout<<minn<<endl;
minn=mymin(minn,cost);
return;
}
for(int i=1;i<=k;i++)
{
if(vis[i]) continue;
vis[i]=1;
//cout<<i<<' '<<time<<endl;
dfs(i,time+1,cost+mapp[line[pos]][line[i]]);
vis[i]=0;
}
}
int main()
{
int t;
cin.sync_with_stdio(false);
while(cin>>t&&t)
{
for(int i=0;i<t;i++) for(int j=0;j<t;j++) cin>>mapp[i][j];
cin>>k;
for(int i=1;i<=k;i++) cin>>line[i],vis[i]=0;
minn=inf;
dfs(0,1,0);
cout<<minn<<endl;
}
return 0;
}