USACO Section 2.1 Healthy Holsteins
来源:互联网 发布:csgo国服连接不上网络 编辑:程序博客网 时间:2024/04/29 20:13
题目原文
Burch & Kolstad
Farmer John prides himself on having the healthiest dairy cows in the world. He knows the vitamin content for one scoop of each feed type and the minimum daily vitamin requirement for the cows. Help Farmer John feed his cows so they stay healthy while minimizing the number of scoops that a cow is fed.
Given the daily requirements of each kind of vitamin that a cow needs, identify the smallest combination of scoops of feed a cow can be fed in order to meet at least the minimum vitamin requirements.
Vitamins are measured in integer units. Cows can be fed at most one scoop of any feed type. It is guaranteed that a solution exists for all contest input data.
PROGRAM NAME: holstein
INPUT FORMAT
Line 1:integer V (1 <= V <= 25), the number of types of vitaminsLine 2:V integers (1 <= each one <= 1000), the minimum requirement for each of the V vitamins that a cow requires each dayLine 3:integer G (1 <= G <= 15), the number of types of feeds availableLines 4..G+3:V integers (0 <= each one <= 1000), the amount of each vitamin that one scoop of this feed contains. The first line of these G lines describes feed #1; the second line describes feed #2; and so on.SAMPLE INPUT (file holstein.in)
4100 200 300 400350 50 50 50200 300 200 300900 150 389 399
OUTPUT FORMAT
The output is a single line of output that contains:
- the minimum number of scoops a cow must eat, followed by:
- a SORTED list (from smallest to largest) of the feed types the cow is given
SAMPLE OUTPUT (file holstein.out)
2 1 3
分析
提交代码
/*ID: Aaron CaiPROG: holsteinLANG: C++*/#include <iostream>#include <fstream>#include <vector>#include <algorithm>#include <string>#include <math.h>#include <limits>#include <map>using namespace std;int V,G;vector<int> req;vector<vector<int> > feeds;vector<int> feeded;vector<int> result;int minscoop=16;bool satisfy(){if(req.size() != feeded.size())return false;bool out = true;for (int i=0;i!=req.size();i++){out = out && req[i]<=feeded[i] ;if(!out)return out;}return out;}void search(int scoop,int scoopcnt,vector<int> &record){if(scoop+1 > G)return;for (int i=0;i!=V;i++){feeded[i] += feeds[scoop][i];}record.push_back(scoop);if(satisfy()){if(scoopcnt+1 < minscoop){minscoop = scoopcnt + 1;result.clear();result = record;}}else{search(scoop+1,scoopcnt+1,record);}for (int i=0;i!=V;i++){feeded[i] -= feeds[scoop][i];}record.pop_back();search(scoop+1,scoopcnt,record);}int main(){ifstream fin("holstein.in");ofstream fout("holstein.out");fin>>V;req.resize(V);feeded.resize(V,0);for (int i=0;i!=V;i++){fin >> req[i];}fin >>G;feeds.resize(G);for (int i=0;i!=G;i++){feeds[i].resize(V);for(int j=0;j!=V;j++){fin >> feeds[i][j];}}result.resize(15);vector<int> record;search(0,0,record);fout << minscoop << " ";for (int i=0;i!=result.size()-1;i++){fout<< result[i]+1 << " ";}fout << result[result.size()-1]+1 << endl;return 0;}
提交结果
TASK: holsteinLANG: C++Compiling...Compile: OKExecuting... Test 1: TEST OK [0.008 secs, 3504 KB] Test 2: TEST OK [0.008 secs, 3504 KB] Test 3: TEST OK [0.005 secs, 3504 KB] Test 4: TEST OK [0.005 secs, 3504 KB] Test 5: TEST OK [0.005 secs, 3504 KB] Test 6: TEST OK [0.008 secs, 3504 KB] Test 7: TEST OK [0.008 secs, 3504 KB] Test 8: TEST OK [0.011 secs, 3504 KB] Test 9: TEST OK [0.011 secs, 3504 KB] Test 10: TEST OK [0.016 secs, 3504 KB]All tests OK.
官方参考答案
#include <stdio.h>#include <assert.h>#define MAXV 25#define MAXF 15int req[MAXV]; /* the vitamin requirements */int numv; /* number of vitamins */int feeds[MAXF][MAXV]; /* the vitamin within each feed */int numf; /* number of feeds */int best; /* the minimum number of feeds to use found thus far */int bestf[MAXF]; /* the set */int curf[MAXF]; /* the current set of feeds being considered */void find_feed(int fcnt, int fid) { /* fcnt is the number of feeds in the current mixture, fid is the identifier of the first feed to try adding (last feed + 1) */ int lv; /* check if the requirement has been met */ for (lv = 0; lv < numv; lv++) if (req[lv] > 0) break; if (lv >= numv) { /* all the requirements are met */ /* we know this is better, since we wouldn't have checked it otherwise (see below) */ best = fcnt; for (lv = 0; lv < best; lv++) bestf[lv] = curf[lv]; return; } while (fid < numf && fcnt+1 < best) { /* try adding each feed to the mixture */ /* the fcnt+1 < best ensures that we stop if there's no hopein finding a better solution than one found already */ /* add the vitamins from this feed */ for (lv = 0; lv < numv; lv++) req[lv] -= feeds[fid][lv]; curf[fcnt] = fid; /* put it in the list */ find_feed(fcnt+1, fid+1); /* undo adding the vitamins */ for (lv = 0; lv < numv; lv++) req[lv] += feeds[fid][lv]; /* next feed */ fid++; } }int main(void) { FILE *fin, *fout; int lv, lv2; fin = fopen("holstein.in", "r"); fout = fopen("holstein.out", "w"); assert(fin); assert(fout); fscanf (fin, "%d", &numv); for (lv = 0; lv < numv; lv++) fscanf (fin, "%d", &req[lv]); fscanf (fin, "%d", &numf); for (lv = 0; lv < numf; lv++) for (lv2 = 0; lv2 < numv; lv2++) fscanf (fin, "%d", &feeds[lv][lv2]); best = numf+1; find_feed(0, 0); fprintf (fout, "%i", best); for (lv = 0; lv < best; lv++) fprintf (fout, " %i", bestf[lv]+1); fprintf (fout, "\n"); return 0; }
THE END
- USACO Section 2.1 Healthy Holsteins
- USACO Section 2.1 Healthy Holsteins
- USACO Section 2.1 - Healthy Holsteins 二进制枚举
- Section 2.1 Healthy Holsteins
- USACO 2.1 Healthy Holsteins
- USACO 2.1-Healthy Holsteins
- USACO 2.1 Healthy Holsteins
- USACO section 2.1 Healthy Holsteins(枚举,二进制存储)
- USACO-Section 2.1 Healthy Holsteins (状态压缩DFS)
- USACO 2.1.5 Healthy Holsteins
- USACO 2.1.4Healthy Holsteins
- [USACO 2.1.4] Healthy Holsteins
- usaco 2.1.4 Healthy Holsteins
- USACO 2.1 Healthy Holsteins (holstein)
- USACO 2.1.4 Healthy Holsteins
- USACO 2.1.4 Healthy Holsteins
- USACO 2.1 Healthy Holsteins (DFS)
- USACO 2.1.4 Healthy Holsteins
- va_start和va_end的使用
- K-均值聚类算法
- 字符集
- 平衡二叉树实现-旋转
- HDU5086Revenge of Segment Tree(数论)
- USACO Section 2.1 Healthy Holsteins
- 三大WEB服务器对比分析(apache ,lighttpd,nginx)
- 好好学习,天天向上!
- JS高级心法——作用域链
- CLR:将System.Net命名空间引用到C++里面
- 2014/11/2 常用正则表达式的参考
- Android4.4 Framework分析——Launcher中启动应用程序(startActivity)的过程
- Linux 不需要磁盘清理
- 字对齐