#include<stdio.h> #include<string.h> #define maxn 33 int n,k,a,mini,RK; int mapp[maxn][maxn],w[maxn]; void dfs(int station,int coun,int dis){ if(coun==RK){ if(mini>dis) mini=dis; } for(int i=1;i<n;i++){ if(w[i]!=0){ w[i]=0; dfs(i,coun+1,dis+mapp[station][i]); w[i]=1; } } } int main(){ while(scanf("%d",&n)){ mini=31000; if(n==0) break; RK=0; memset(mapp,0,sizeof(mapp)); memset(w,0,sizeof(w)); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ scanf("%d",&mapp[i][j]); } } scanf("%d",&k); for(int i=0;i<k;i++){ scanf("%d",&a); if(w[a]==0) { RK++; w[a]=1; } } dfs(0,0,0); printf("%d\n",mini); } return 0; }
时间: 2024-10-22 15:22:36