NYOJ-103-backpack Problems

Source: Internet
Author: User

Question address: http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 106

 

Backpack problem time limit: 3000 MS | memory limit: 65535 kb difficulty: 3
Description
Now there are a lot of items (they can be separated), and we know the value V and weight W of each item (1 <= V, W <= 10 ); if you have a backpack that can hold the weight of M (10 <= m <= 20), all you need to do is to pack the item into the backpack, this maximizes the total value of items in a backpack.
Input
Enter a positive integer N (1 <=n <= 5) In the first line, indicating that N groups of test data exist;
Then there is n test data. The first row of each group of test data has two positive integers S, M (1 <= S <= 10), and s indicates that there are s items. Each row in the next s line has two positive integers V, W.
Output
Outputs the value and value of the items in the backpack in each group of test data. Each output occupies one row.
Sample Input
1 3 15 5 10 2 8 3 9 
Sample output
65

 

Solution report: the most basic knapsack problem, starting from the most valuable, can be loaded, full. We recommend that you read the nine articles about backpacks.

 

1

2 # include <stdio. h>
3 # include <string. h>
4 # include <stdlib. h>
5
6 struct P {
7 int V;
8 int W;
9} p [15];
10
11 int CMP (const void * a, const void * B ){
12 struct p * c = (struct p *);
13 struct p * D = (struct p *) B;
14 return D-> V-C-> V;
15}
16
17 int main (){
18 int N, S, M, A, B, I, ans;
19 scanf ("% d", & N );
20 while (n --){
21 scanf ("% d", & S, & M );
22 For (I = 0; I <s; I ++ ){
23 scanf ("% d", & P [I]. V, & P [I]. W );
24}
25 qsort (P, S, sizeof (P [0]), CMP );
26 I = ans = 0;
27 while (M> = P [I]. W ){
28 ans + = P [I]. W * P [I]. V;
29 m-= P [I]. W;
30 I ++;
31}
32 ans + = m * P [I]. V;
33 printf ("% d \ n", ANS );
34}
35 return 0;
36}
37

NYOJ-103-backpack Problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.