USACO 2.1 Healthy Holsteins(枚举)

本来以为是一个好高深的DP的,看了看数据范围,枚举啊。。。n和m分不清了,错了好几次,以后用题目里面的变量,养成好习惯。。。

 1 /*

 2  ID: cuizhe

 3  LANG: C++

 4  TASK: holstein

 5 */

 6 #include <cstdio>

 7 #include <cstring>

 8 #include <cmath>

 9 #include <algorithm>

10 using namespace std;

11 int o[21],c[30],p[20][40],t[30],an[21];

12 int main()

13 {

14     int i,j,k,n,m,num,ans;

15     freopen("holstein.in","r",stdin);

16     freopen("holstein.out","w",stdout);

17     scanf("%d",&n);

18     for(i = 1;i <= n;i ++)

19     scanf("%d",&c[i]);

20     scanf("%d",&m);

21     for(i = 1;i <= m;i ++)

22     {

23         for(j = 1;j <= n;j ++)

24         scanf("%d",&p[i][j]);

25     }

26     ans = 10000;

27     for(i = 1;i < (1<<m);i ++)

28     {

29         memset(t,0,sizeof(t));

30         memset(o,0,sizeof(o));

31         num = 0;

32         for(j = 0;j < m;j ++)

33         {

34             if((1<<j)&i)

35             {

36                 o[j+1] = 1;

37                 for(k = 1;k <= n;k ++)

38                 t[k] += p[j+1][k];

39                 num ++;

40             }

41         }

42         for(j = 1;j <= n;j ++)

43         {

44             if(t[j] < c[j]) break;

45         }

46         if(j == n+1)

47         {

48             if(ans > num)

49             {

50                 ans = num;

51                 for(j = 1;j <= m;j ++)

52                 {

53                     an[j] = o[j];

54                 }

55             }

56             else if(ans == num)

57             {

58                 for(j = 1;j <= m;j ++)

59                 {

60                     if(!an[j]&&o[j])

61                     {

62                         for(k = 1;k <= n;k ++)

63                         an[k] = o[k];

64                         break;

65                     }

66                     else if(an[j]&&!o[j])

67                     break;

68                 }

69             }

70         }

71     }

72     printf("%d",ans);

73     for(i = 1;i <= m;i ++)

74     {

75         if(an[i]) printf(" %d",i);

76     }

77     printf("\n");

78     return 0;

79 }

你可能感兴趣的:(health)