Knapsack Problem time limit:MS | Memory limit:65535 KB Difficulty:3
-
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 I give you a backpack, it can hold a weight of M (10<=m<=20), All you have to do is pack the items into your backpack so that the value of the items in your 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
-
-
Source
-
-
[Miao Building] Original
-
-
Uploaded by
-
-
Miao-dong building
-
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 5 structAC6 {7 intA, B;8} num[ the];9 Ten BOOLCMP (AC A, AC b) One { A returnA.A >B.A; - } - the intMain () - { - intT; -scanf"%d", &t); + while(t--) - { + intN, M; Ascanf"%d%d", &n, &m); at for(intI=0; i<n; i++) -scanf"%d%d", &NUM[I].A, &num[i].b); -Sort (num, num+N, CMP); - intsum =0, total =0; - for(intI=0; i<m; i++) - { insum + = num[i].b; Total + = num[i].a*num[i].b; - //printf ("%d%d\n", sum, total); to if(Sum >m) + { -Total-= (sum-m) *num[i].a; the Break; * } $ if(Sum = =m)Panax Notoginseng Break; - } theprintf"%d\n", total); + } A return 0; the}
-
Nanyang 106--knapsack problem