原题链接
http://www.51mxd.cn/problem.php-pid=106.htm
考察贪心算法,按重量或价值排序都不行,每次都要取最多,所以按单位价值排序,
#include "iostream"
#include "algorithm"
using namespace std;
struct node{
int v;
int w;
}a[100];
int cmp(struct node a,struct node b)
{
return a.v>b.v;
}
int main()
{
int T;
cin>>T;
while(T--)
{
int s,m;
int v,w;
scanf("%d%d",&s,&m);
for(int i=0;i<s;i++)
{
scanf("%d%d",&(a[i].v),&(a[i].w));
}
sort(a,a+s,cmp);
int sumvalue=0,weight=m;
for(int i=0;i<s;i++)
{
if(a[i].w<=weight)
{
sumvalue+=a[i].v*a[i].w ;
weight-=a[i].w;
}
else{
sumvalue+=a[i].v*weight;
break;
}
}
cout<<sumvalue<<endl;
}
return 0;
}