Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=1572

生存排列枚举

我的代码
 1 #include <cstdio>
2 #include <algorithm>
3 using namespace std;
4 const int N=40,INF=10000000;
5 int a[N][N],p[10],b[10];
6 int main()
7 {
8 int n,k,ans,s,i,j;
9 while (scanf("%d",&n),n)
10 {
11 ans=INF;
12 for (i=0;i<n;i++)
13 for (j=0;j<n;j++) scanf("%d",&a[i][j]);
14 scanf("%d",&k);
15 for (i=1;i<=k;i++) scanf("%d",&b[i]);
16 for (i=1;i<=k;i++) p[i]=i; p[0]=0;
17 do
18 {
19 s=0;
20 for (i=1;i<=k;i++) s+=a[b[p[i-1]]][b[p[i]]];
21 if (s<ans) ans=s;
22 }while (next_permutation(p+1,p+k+1));
23 printf("%d\n",ans);
24 }
25 return 0;
26 }

 

posted on 2012-01-15 14:27  Qiuqiqiu  阅读(281)  评论(0编辑  收藏  举报