## USACO 1.3.1 Mixing Milk

November 4, 2010 Leave a comment

I’ve been doing the USACO Training Program (a programming contest training program) lately so I have decided to share some solutions on this blog for anyone having trouble (I know I had a lot of trouble solving these problems…). This problem is called **Mixing Milk**, it’s the first question from section 3.

——

Since milk packaging is such a low margin business, it is important to keep the price of the raw product (milk) as low as possible. Help Merry Milk Makers get the milk they need in the cheapest possible manner.

The Merry Milk Makers company has several farmers from which they may buy milk, and each one has a (potentially) different price at which they sell to the milk packing plant. Moreover, as a cow can only produce so much milk a day, the farmers only have so much milk to sell per day. Each day, Merry Milk Makers can purchase an integral amount of milk from each farmer, less than or equal to the farmer’s limit.

Given the Merry Milk Makers’ daily requirement of milk, along with the cost per gallon and amount of available milk for each farmer, calculate the minimum amount of money that it takes to fulfill the Merry Milk Makers’ requirements.

Note: The total milk produced per day by the farmers will be sufficient to meet the demands of the Merry Milk Makers.

### PROGRAM NAME: milk

### INPUT FORMAT

Line 1: | Two integers, N and M. The first value, N, (0 <= N <= 2,000,000) is the amount of milk that Merry Milk Makers’ want per day. The second, M, (0 <= M <= 5,000) is the number of farmers that they may buy from. |

Lines 2 through M+1: | The next M lines each contain two integers, P_{i} and A_{i}.P _{i} (0 <= P_{i} <= 1,000) is price in cents that farmer i charges.A _{i} (0 <= Ai <= 2,000,000) is the amount of milk that farmer i can sell to Merry Milk Makers per day. |

### SAMPLE INPUT (file milk.in)

100 5 5 20 9 40 3 10 8 80 6 30

### OUTPUT FORMAT

A single line with a single integer that is the minimum price that Merry Milk Makers can get their milk at for one day.

### SAMPLE OUTPUT (file milk.out)

630

## SOLUTION

This is one algorithm to solve this problem:

- Sort the farmers by cost
- Buy from the farmers (starting from the lowest cost) until the amount of milk wanted is reached.

This algorithm works because when you buy from the lowest cost available, the milk you’re buying is of the best value. As long as you’re always buying milk of the best value, the amount of money you spend is minimal.

/* ID: frigid.1 LANG: JAVA TASK: milk */ import java.io.*; import java.util.*; class Farmer implements Comparable { int cost, milk; public Farmer (int c, int m) { cost = c; milk = m; } public int compareTo (Farmer o) { return cost - o.cost; } } //Farmer class public class milk { public static void main (String[] args) throws IOException { BufferedReader f = new BufferedReader (new FileReader ("milk.in")); PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter ("milk.out"))); int minPrice = 0; StringTokenizer st = new StringTokenizer (f.readLine()); int milkWanted = Integer.parseInt (st.nextToken()); int n = Integer.parseInt (st.nextToken()); Farmer[] farmers = new Farmer[n]; for (int i = 0; i < n; i++) { st = new StringTokenizer (f.readLine()); farmers[i] = new Farmer (Integer.parseInt (st.nextToken()), Integer.parseInt (st.nextToken())); } //sort farmers Arrays.sort(farmers); //buy from farmers (starting from lowest cost) until goal is reached for (int i = 0; i < n && milkWanted > 0; i++) { if (milkWanted >= farmers[i].milk) { minPrice += farmers[i].cost * farmers[i].milk; milkWanted -= farmers[i].milk; } else { minPrice += farmers[i].cost * milkWanted; milkWanted = 0; } } out.println(minPrice); out.close(); } //main method } //milk class

This problem can also be done without using a Farmer object. Instead of having a list of Farmer objects, you can make an integer array of size 1001 where each index is a price and add the amount of milk to each index. The sorting algorithm used here is called Counting Sort. Here is the above program modified in this way…

/* ID: frigid.1 LANG: JAVA TASK: milk */ import java.io.*; import java.util.*; public class milk { public static void main (String[] args) throws IOException { BufferedReader f = new BufferedReader (new FileReader ("milk.in")); PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter ("milk.out"))); int minPrice = 0; StringTokenizer st = new StringTokenizer (f.readLine()); int milkWanted = Integer.parseInt (st.nextToken()); int n = Integer.parseInt (st.nextToken()); int[] farmers = new int [1001]; for (int i = 0; i < n; i++) { st = new StringTokenizer (f.readLine()); farmers[Integer.parseInt (st.nextToken())] += Integer.parseInt (st.nextToken()); } //buy from farmers (starting from lowest cost) until goal is reached for (int i = 0; i < 1001 && milkWanted > 0; i++) { if (milkWanted >= farmers[i]) { minPrice += i * farmers[i]; milkWanted -= farmers[i]; } else { minPrice += i * milkWanted; milkWanted = 0; } } out.println(minPrice); out.close(); } //main method } //milk class