师大 11370

http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11370&courseid=0

1
3
0 -3 1
3 0 -2
-1 2 0


1 3

 反复思考...

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<queue>
 6 using namespace std;
 7 
 8 int a[21][21],sum[21];
 9 bool hash[(1<<20)+2];
10 int n;
11 
12 void dfs(int k)
13 {
14     hash[k]=true;
15     int i,j;
16     for(i=0;i<n;i++)
17     {
18         if( ((1<<i)&k)>0 && sum[i]>0 && hash[k-(1<<i)]==false)
19         {
20             for(j=0;j<n;j++)
21             {
22                 if(k&(1<<j))
23                     sum[j]=sum[j]-a[j][i];
24             }
25             dfs((k-(1<<i)));
26             for(j=0;j<n;j++)
27             {
28                 if(k&(1<<j))
29                     sum[j]=sum[j]+a[j][i];
30             }
31         }
32     }
33 }
34 int main()
35 {
36     int T,i,j,t;
37     bool flag;
38     scanf("%d",&T);
39     while(T--)
40     {
41         scanf("%d",&n);
42         for(i=0;i<n;i++)
43         for(j=0;j<n;j++)
44             scanf("%d",&a[i][j]);
45         memset(hash,false,sizeof(hash));
46         for(i=0;i<n;i++)
47         {
48             sum[i]=0;
49             for(j=0;j<n;j++)
50                 sum[i]=sum[i]+a[i][j];
51         }
52         dfs((1<<n)-1);
53         t=0;flag=false;
54         for(i=0;i<n;i++)
55         {
56             if(hash[1<<i]==true)
57             {
58                 if(t==1)printf(" ");
59                 printf("%d",i+1);
60                 flag=true;
61                 t=1;
62             }
63         }
64         if(!flag)printf("0");
65         printf("
");
66     }
67     return 0;
68 }
原文地址:https://www.cnblogs.com/tom987690183/p/3649841.html