p1460 Healthy Holsteins

列举所有的子集找最优就行。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#define mkp make_pair
using namespace std;
const double EPS=1e-8;
typedef long long lon;
const lon SZ=30,INF=0x7FFFFFFF;
int v[SZ],arr[SZ][SZ];

inline int lowbit(int x)
{
    return x&-x;
}

int getdgt(int x)
{
    int res=0;
    for(;x;x-=lowbit(x),++res);
    return res;
}

bool chk(int x,int n)
{
    int tmp[SZ];
    memset(tmp,0,sizeof(tmp));
    for(int i=0;i<31;++i)
    {
        if(x&(1<<i))
        {
            for(int j=0;j<n;++j)
            {
                tmp[j]+=arr[i][j];
            }
        }
    }
    for(int j=0;j<n;++j)
    {
        if(tmp[j]<v[j])return 0;
    }
    return 1;
}

int main()
{
    std::ios::sync_with_stdio(0);
    //freopen("d:\\1.txt","r",stdin);
    lon casenum;
    //cin>>casenum;
    //for(lon time=1;time<=casenum;++time)
    {
        int n;
        cin>>n;
        for(int i=0;i<n;++i)cin>>v[i];
        int m;
        cin>>m;
        for(int i=0;i<m;++i)
        {
            for(int j=0;j<n;++j)
            {
                cin>>arr[i][j];
            }
        }
        int mind=INF,res=0;
        for(int i=0;i<(1<<m);++i)
        {
            if(getdgt(i)<mind&&chk(i,n))
            {
                mind=getdgt(i);
                res=i;
            }
        }
        cout<<mind;
        for(int i=0;i<31;++i)
        {
            if(res&(1<<i))cout<<" "<<i+1;
        }
        cout<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/gaudar/p/9822202.html

时间: 2024-08-08 19:17:46

p1460 Healthy Holsteins的相关文章

2.1.4 Healthy Holsteins 健康的好斯坦奶牛

2.1.4 Healthy Holsteins 健康的好斯坦奶牛 一.题目描述 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保持它们的健康,使喂给牛的饲料的种数最少. 给出牛所需的最低的维他命量,输出喂给牛需要哪些种类的饲料,且所需的饲料剂量最少. 维他命量以整数表示,每种饲料最多只能对牛使用一次,数据保证存在解. PROGRAM NAME: holstein INPUT FORMAT 第1 行:一个整数V(1<=V<

USACO Section 2.1 Healthy Holsteins

/* ID: lucien23 PROG: holstein LANG: C++ */ #include <iostream> #include <fstream> #include <vector> using namespace std; bool compFun(int x, int y) { int temp, i = 0; while (true) { temp = 1 << i; if (temp&x > temp&y) {

洛谷P1460 健康的荷斯坦奶牛 Healthy Holsteins

题目描述 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保持它们的健康,使喂给牛的饲料的种数最少. 给出牛所需的最低的维他命量,输出喂给牛需要哪些种类的饲料,且所需的饲料剂量最少. 维他命量以整数表示,每种饲料最多只能对牛使用一次,数据保证存在解. 输入输出格式 输入格式: 第1行:一个整数V(1<=V<=25),表示需要的维他命的种类数. 第2行:V个整数(1<=每个数<=1000),表示牛每天需要的每种

洛谷P1460 [USACO2.1]健康的荷斯坦奶牛 Healthy Holsteins

题目描述 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保持它们的健康,使喂给牛的饲料的种数最少. 给出牛所需的最低的维他命量,输出喂给牛需要哪些种类的饲料,且所需的饲料剂量最少. 维他命量以整数表示,每种饲料最多只能对牛使用一次,数据保证存在解. 输入输出格式 输入格式: 第1行:一个整数V(1<=V<=25),表示需要的维他命的种类数. 第2行:V个整数(1<=每个数<=1000),表示牛每天需要的每种

usaco Healthy Holsteins

题意为有一头牛,它每天所需的维生素有V种,每种维生素的需求量给出,同时给出G种已知的饲料,求如何组合这些饲料可以使得牛获得足够的维生素: 给的数据范围很小,直接暴力枚举所有 /* ID: modengd1 PROG: holstein LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> using namespace std; int V,G,food[15][1000],cow

USACO Section2.1 Healthy Holsteins 解题报告 【icedream61】

holstein解题报告 ------------------------------------------------------------------------------------------------------------------------------------------------[题目] 你需要给一头奶牛制定最优的喂养计划. 有V种维他命,每天对于每种维他命,牛都有需要达到的指标: 同时你有G种饲料,编号依次为1到G,每种维他命在每种饲料中所蕴含的量都会给你.

【USACO 2.1】Healthy Holsteins

/* TASK: holstein LANG: C++ URL: http://train.usaco.org/usacoprob2?a=SgkbOSkonr2&S=holstein SOLVE: con[i][j]为食物i含有维生素j的量,ned[i]为需要的维生素i的量 bfs,用二进制保存状态 */ #include<cstdio> #define N 30 int v,g,ned[N],con[N][N]; int now[N]; int l,r,q[40000]; bool

USACO Healthy Holsteins DFS

使用排列组合,遍历所有可能的情况C(1)+C(2)+C(3)……C(n)= 2^G种组合 数据规模不大,暴力过去最多也就是2^15 = 23768种情况 所以就暴力咯,不过还是Debug了一会 Source Code: /* ID: wushuai2 PROG: holstein LANG: C++ */ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #i

USACO-Section 2.1 Healthy Holsteins (状态压缩DFS)

描述 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保持它们的健康,使喂给牛的饲料的种数最少. 给出牛所需的最低的维他命量,输出喂给牛需要哪些种类的饲料,且所需的饲料剂量最少. 维他命量以整数表示,每种饲料最多只能对牛使用一次,数据保证存在解. 格式 PROGRAM NAME: holstein INPUT FORMAT: (file holstein.in) 第1行:一个整数V(1<=V<=25),表示需要的维他命的