USACO1.3.1 Mixing Milk(混合牛奶)

来源:互联网 发布:西南大学网络教育论坛 编辑:程序博客网 时间:2024/04/27 15:04

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

题目大意:已知同种物品在不同商家处的单价和供应量,求满足额定需求量所支出的最小花费。

解题思路:简单的贪心算法:将所有的牛奶按价格升序排列(用快排),然后从低到高买入,直到买够m为止。

最佳解题方法:因为价格的范围在0到1000之内,所以,我们只要在读入数据的时候把相同价格的合并即可,进行计算时,也只需要i从0到1000进行扫描,若有价格为i的牛奶收购即可,直到买够m为止。这样就不需要排序了。

【注意】:每天农民生产的牛奶的总数对快乐的牛奶制造者来说足够的。

以下是我的代码:

/*USER:xingwen wangTASK:milkLANG:C++ */#include<cstdio>#include<algorithm>struct node{     int p,a;}farm[5001];bool cmp(node b,node c){     return b.p<c.p;}int main(){     int i,N,M,ans;     freopen("milk.in","r",stdin);     freopen("milk.out","w",stdout);     scanf("%d%d",&N,&M);     for(i=0;i<M;i++)     scanf("%d%d",&farm[i].p,&farm[i].a);     std::sort(farm,farm+M,cmp);     ans=0;     i=0;     while(N>=farm[i].a&&i<M-1)     {           ans+=farm[i].p*farm[i].a;           N-=farm[i].a;           i++;     }     ans+=N*farm[i].p;     printf("%d\n",ans);     return 0;}

对于这道题来说,其实没有必要排序,相反,不排序效率更好,详见代码:

/*USER:xingwen wangTASK:milkLANG:C*/#include<stdio.h>int main(){     int i,N,M,P,A,ans,a[1002]={0};     freopen("milk.in","r",stdin);     freopen("milk.out","w",stdout);     scanf("%d%d",&N,&M);     for(i=0;i<M;i++)     {           scanf("%d%d",&P,&A);           a[P]+=A;     }     ans=0;     i=0;     while(N>=a[i]&&i<1000)     {           ans+=i*a[i];           N-=a[i];           i++;     }     ans+=N*i;     printf("%d\n",ans);     return 0;}
原创粉丝点击