S 1.3 milk C程序

来源:互联网 发布:递归迷宫生成算法 编辑:程序博客网 时间:2024/06/09 20:15

题目连接:

http://ace.delos.com/usacoprob2?a=i3mjJGbYp0n&S=milk

 

Mixing Milk

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 wants 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, Pi and Ai.
Pi (0 <= Pi <= 1,000) is price in cents that farmer i charges.
Ai (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 55 209 403 108 806 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


 

题目翻译:

描述

由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要。帮助Marry乳业找到最优的牛奶采购方案。

Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是不同的。此外,就像每头奶牛每天只能挤出固定数量的奶,每位奶农每天能提供的牛奶数量是一定的。每天Marry乳业可以从奶农手中采购到小于或者等于奶农最大产量的整数数量的牛奶。

给出Marry乳业每天对牛奶的需求量,还有每位奶农提供的牛奶单价和产量。计算采购足够数量的牛奶所需的最小花费。

注:每天所有奶农的总产量大于Marry乳业的需求量。

[编辑]格式

PROGRAM NAME: milk

INPUT FORMAT:file milk.in

第 1 行共二个数值:N,(0<=N<=2,000,000)是需要牛奶的总数;M,(0<= M<=5,000)是提供牛奶的农民个数。

第 2 到 M+1 行:每行二个整数:Pi 和 Ai

Pi(0<= Pi<=1,000) 是农民 i 的牛奶的价格。

Ai(0 <= Ai <= 2,000,000)是农民 i 一天能卖给Marry的牛奶制造公司的牛奶数量。

OUTPUT FORMAT:file milk.out

单独的一行包含单独的一个整数,表示Marry的牛奶制造公司拿到所需的牛奶所要的最小费用

[编辑]SAMPLE INPUT

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

[编辑]SAMPLE OUTPUT

630

 

 

代码测试结果:

 

USER: Yun Li [smilecl1]
TASK: milk
LANG: C

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 2144 KB]
   Test 2: TEST OK [0.000 secs, 2144 KB]
   Test 3: TEST OK [0.000 secs, 2144 KB]
   Test 4: TEST OK [0.000 secs, 2144 KB]
   Test 5: TEST OK [0.000 secs, 2144 KB]
   Test 6: TEST OK [0.000 secs, 2144 KB]
   Test 7: TEST OK [0.000 secs, 2144 KB]
   Test 8: TEST OK [0.000 secs, 2144 KB]

All tests OK.
Your program ('milk') produced all correct answers!  This is your
submission #2 for this problem.  Congratulations!

 

/*
ID:smilecl1
LANG:C
TASK:milk
*/

/*
TIME:2012/11/12
EMAIL:
SmileCloud201@gmail.com
NAME: LiYun
*/

/*
 * 思路:
 * 先把所有的农民的牛奶按照价格的高低,从低到高排序,这里直接用快排实现。然后价格低的全部购买,直到数量达到需求。
 */

#include <stdio.h>
#define max_number 5000

typedef struct peasant Peasant;

struct peasant
{
    int price;
    int amount;
};

int cmp(const Peasant *va, const Peasant *vb)
{
 return va->price - vb->price;
}


int main()
{
    int i = 0;
    int milk_want;
    Peasant peasant[max_number];
    int peasant_number;
    int sum_price = 0;
    int sum_amount = 0;
    int temp_amount = 0;
    FILE *fin;
    FILE *fout;

    fin = fopen("milk.in", "r");
    fout = fopen("milk.out", "w");
    fscanf(fin, "%d%d", &milk_want, &peasant_number);
    for ( i = 0; i < peasant_number; ++i )
    {
        fscanf(fin, "%d%d", &peasant[i].price, &peasant[i].amount);
    }
    qsort(peasant, peasant_number, sizeof(peasant[0]), cmp);
    if (milk_want == 0)
        fprintf(fout, "%d\n", sum_price);
    else
    {
        for ( i = 0; i < peasant_number; ++i )
        {
            if (sum_amount + peasant[i].amount < milk_want)
            {
                temp_amount = peasant[i].amount;
            }
            else
            {
                temp_amount = milk_want - sum_amount;
            }
            sum_amount += temp_amount;
            sum_price += peasant[i].price * temp_amount;
            if ( milk_want == sum_amount )
            {
                fprintf(fout, "%d\n", sum_price);
                break;
            }
        }
    }
    return 0;
}