dfs,好像有人说过其实搜索就是枚举,这下领会到了
其实bfs应该更好的吧
1 /* ID:linyvxi1
2 PROB:holstein
3 LANG:C++
4 */
5 #include <stdio.h>
6 int v[26];
7 typedef struct Vit{
8 int num[26];
9 }Vit;
10 Vit vit[16];
11 bool visited[16]={false};
12 int final_result[16]={0};
13 int V,G;
14 int MIN=0x7fffffff;
15 int cur_num[26]={0};
16 bool checked_ok()
17 {
18 int i;
19 for(i=0;i<V;i++){
20 if(cur_num[i]<v[i]){
21 return false;
22 }
23 }
24 return true;
25 }
26 void _do(int _num)
27 {
28 int i;
29 for(i=0;i<V;i++){
30 cur_num[i]+=vit[_num].num[i];
31 }
32 }
33 void _un_do(int _num)
34 {
35 int i;
36 for(i=0;i<V;i++){
37 cur_num[i]-=vit[_num].num[i];
38 }
39 }
40
41
42 void dfs(int s_num,int kinds)
43 {
44 if(s_num>G){
45 return;
46 }
47 if(kinds>MIN){
48 return;
49 }
50 if(checked_ok()){
51 if(kinds<MIN){
52 MIN=kinds;
53 int i;
54 int j=0;
55 for(i=0;i<G;i++){
56 if(visited[i]){
57 final_result[j++]=i+1;
58 }
59 }
60 }
61 return;
62 }
63 visited[s_num]=true;
64 _do(s_num);
65 dfs(s_num+1,kinds+1);
66 visited[s_num]=false;
67 _un_do(s_num);
68 dfs(s_num+1,kinds);
69 }
70
71
72 int main()
73 {
74 freopen("holstein.in","r",stdin);
75 freopen("holstein.out","w",stdout);
76 scanf("%d",&V);
77 int i;
78 for(i=0;i<V;i++){
79 scanf("%d",&v[i]);
80 }
81 scanf("%d",&G);
82 int j;
83 for(i=0;i<G;i++){
84 for(j=0;j<V;j++){
85 scanf("%d",&vit[i].num[j]);
86 }
87 }
88 dfs(0,0);
89 printf("%d ",MIN);
90 for(i=0;i<MIN;i++){
91 printf("%d",final_result[i]);
92 if(i!=MIN-1) putchar(' ');
93 }
94 putchar('\n');
95 }