2017-05-31
题目大意:
由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要。帮助Marry乳业找到最优的牛奶采购方案。
Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是不同的。此外,就像每头奶牛每天只能挤出固定数量的奶,每位奶农每天能提供的牛奶数量是一定的。每天Marry乳业可以从奶农手中采购到小于或者等于奶农最大产量的整数数量的牛奶。
给出Marry乳业每天对牛奶的需求量,还有每位奶农提供的牛奶单价和产量。计算采购足够数量的牛奶所需的最小花费。
注:每天所有奶农的总产量大于Marry乳业的需求量。
样例输入:
100 5
5 20
9 40
3 10
8 80
6 30
样例输出:
630
题解:
对单价进行排序,使用贪心算法
代码:
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int m , n;
struct milkInfo{
int price;
int amount;
};
milkInfo mil[5005];
bool cmp(milkInfo a,milkInfo b){
return a.price < b.price;
}
int main(){
ofstream cout("milk.out");
ifstream cin("milk.in");
cin >> n >> m;
if(n == 0 || m == 0){
cout << "0" << endl;
return 0;
}
for(int i = 0;i < 5005;i++){
mil[i].price = 0;
mil[i].amount = 0;
}
for(int i = 0;i < m;i++){
cin >> mil[i].price >> mil[i].amount;
}
sort(mil,mil+m,cmp);
int tmp = n;
int flag = 0;
int sum = 0;
while(tmp >= mil[flag].amount){
sum += mil[flag].amount * mil[flag].price;
tmp -= mil[flag].amount;
flag++;
}
if(tmp == 0)
cout << sum << endl;
else{
sum += mil[flag].price * tmp;
cout << sum << endl;
}
return 0;
}