[USACO]Healthy Holsteins

13 篇文章 0 订阅
Healthy Holsteins
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 vitamins
Line 2:V integers (1 <= each one <= 1000), the minimum requirement for each of the V vitamins that a cow requires each day
Line 3:integer G (1 <= G <= 15), the number of types of feeds available
Lines 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)

4
100 200 300 400
3
50   50  50  50
200 300 200 300
900 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
If more than one set of feedtypes yield a minimum of scoops, choose the set with the smallest feedtype numbers.

SAMPLE OUTPUT (file holstein.out)

2 1 3
 
还是用BFS进行。具体的解题思路和做的clocks这道题无二。而且比上次还简单了,因为每种食物只能用一铲(scoop)。
http://blog.csdn.net/newlaide/article/details/7890701
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
/**
 * 4  --vit_size
 * 100 200 300 400
 * 3  --feed_size
 * 50   50  50  50
 * 200 300 200 300
 * 900 150 389 399
 * 
 * */
public class holstein {
    public static void main(String[] args) throws IOException,FileNotFoundException {
    BufferedReader br = new BufferedReader(new FileReader("holstein.in"));
    FileWriter fout = new FileWriter("holstein.out");
    //how many kinds of vitamin
    int vit_size = Integer.parseInt(br.readLine());
    //the amount of each vitamin that cows needed
    int[]  vits = new int[vit_size+1];
    String[] tmp = br.readLine().split(" +");
    long start = System.currentTimeMillis();
    for(int i =0;i<vit_size;i++) {
        vits[i] = Integer.parseInt(tmp[i]);
    }
    //how many kinds of food
    int feed_size =  Integer.parseInt(br.readLine());
    //each food that contains how much of each vitamin
    int[][]  feeds = new int[feed_size][vit_size];
    //record trace of searching and start point of next searching
    for(int i =0;i<feed_size;i++) {
        tmp = br.readLine().split(" +");
        for(int j =0;j<vit_size;j++)
        {
            feeds[i][j] = Integer.parseInt(tmp[j]);
        }
    }
    
    LinkedList<int[]> vit_queue = new LinkedList<int[]>();
    vit_queue.add(vits);
    LinkedList<int[]> trace_queue = new LinkedList<int[]>();
    trace_queue.add(new int[feed_size+1]);
    while(!vit_queue.isEmpty()) {
        int[] vit_ = vit_queue.poll();
        if(check(vit_)) {
            break;
        }
        int no_used_feed = vit_[vit_size];
        int[] trace = trace_queue.poll();
        int position = trace[feed_size]; //this 'position' represent the trace length in array
//        fout.write(printer(trace)+"\n");
        for(int i=no_used_feed;i<feed_size;i++) {
            int[] vit_t = vit_.clone();
            int[] trace_ = trace.clone();
            feed(vit_t,feeds[i]);
            vit_t[vit_size]=i+1;
            vit_queue.add(vit_t);
            trace_[position]=i;
            trace_[feed_size]++;
//            printer(trace_);
            trace_queue.add(trace_);
        }
    }
    int[] result = trace_queue.poll();
    int size = result[feed_size];
    String result_s = ""+size;
    for(int i =0;i<size;i++) {
        result_s+=" "+(result[i]+1);
    }
    fout.write(result_s+"\n");
    fout.flush();
    fout.close();
    br.close();
    long end = System.currentTimeMillis();
    System.out.println(end-start);
    System.exit(0);
    }

    private static String printer(int[] trace) {
	String result = new String();
	for(int i = 0;i<trace[trace.length-1];i++) {
	    result +=trace[i]+" " ;
	}
	return result;
    }

    private static void feed(int[] vit_, int[] is)
    {
        for(int i =0;i<vit_.length-1;i++) {
            vit_[i]-=is[i];
        }
    }

    private static boolean check(int[] vit_tmp)
    {
        for(int i=0;i<vit_tmp.length-1;i++) {
            if(vit_tmp[i]>0) {
                return false;
            }
        }
        return true;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值