Describe
Now there are a lot of items (they are divisible), we know the value of each unit weight of each item V and the weight w (1<=v,w<=10); If you give you a backpack, it can hold a weight of M (10<=m<=20), all you have to do is pack the items in your backpack. , so that the total value of the items in the backpack is the largest.
-
-
Input
-
-
the first line enters a positive integer n (1<=n<=5), which indicates that there are n sets of test data;
Then there are n test data, the first line of each set of test data has two positive integers s,m (1<=s<=10); s indicates that there is an S item. The next S-line has two positive integer v,w per line.
Output
Outputs the value of the items in the
backpack for each set of test data and each output takes up one row.
Sample input
13 155 102) 83 9
Sample output
65
#include <cstdio> #include <algorithm>using namespace std;struct bag{int v;int W;} Arr[1010];bool cmp (bag A,bag b) {return A.V>B.V;} int main () {int t,i;int n,m;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m), and for (I=0;i<n;++i) {scanf ("%") d%d ", &ARR[I].V,&ARR[I].W);//Here is the unit value}sort (ARR,ARR+N,CMP); int max=m;int sum=0;for (i=0;i<n;++i) {if (arr[i ].w<=max) {SUM+=ARR[I].V*ARR[I].W;MAX-=ARR[I].W;} Else{sum+=max*arr[i].v;break;}} printf ("%d\n", sum); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Nanyang oj106 knapsack problem