See the 01 backpack for nyoj 860 [Alternative 01 backpack]

Source: Internet
Author: User

Analysis:

First, this question cannot be considered a normal 01 backpack problem, because w <= 10 ^ 9 cannot be opened, so the large array certainly has other ideas, observe that we know that the value of V is very small, and the maximum value is 100. Then we can use this to take the sum of values as the sum of the original quality, but pay attention to the following points: because the question is to find out the greatest value within the range of no more than W quality, and we are seeking the quality with the greatest value, we can understand after careful analysis, we need to store a backpack with a certain value of I as small as possible, so we can analyze it in turn to know that the smaller quality has a large value serial number, which completely satisfies the question, the last point is that each value package (going out to DP [0]) must be initialized to a relatively large number, so that the minimum value can be put. If the initialization is 0, it must be wrong (think about it). This is to fill the backpack completely, but one is infinite... (Why is it like this, because we are looking for it in turn)

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

Code:

# Include <stdio. h> # include <string. h ># define maxn 0x3f3f3ftypedef struct {int W, V;} STR; STR s [105]; int DP [10050]; int main () {int n, m; while (scanf ("% d", & N, & M) = 2) {// memset (DP, 'B', sizeof (DP )); this is the same as the following, but I don't know why int sum = 0; For (INT I = 0; I <n; I ++) scanf ("% d ", & S [I]. w, & S [I]. v), sum + = s [I]. v; For (INT I = 0; I <= sum; I ++) DP [I] = maxn; // same effect as above DP [0] = 0; for (INT I = 0; I <n; I ++) for (Int J = sum; j> = s [I]. v; j --) {If (DP [J]> DP [J-s [I]. v] + s [I]. w) DP [J] = DP [J-s [I]. v] + s [I]. W ;}for (INT I = sum; I> = 0; I --) if (DP [I] <= m) {printf ("% d \ n ", i); break ;}} return 0 ;}


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.