Vijos P1836hys and Tanabata Festival big battle (01 backpack 2--convert DP object)

Source: Internet
Author: User
Tags min

Title: Vijos P1836hys and Tanabata festival big battle

Test instructions

n objects, per value VI, specific gravity PI, total capacity 100

Analysis:

Similar to the weight of the Backpack pi is a real number, not as subscript, so change the DP object

Will find the maximum value in capacity 100 → to find the minimum capacity of the corresponding value,

Then the value of the first ≤100 of the volume, which is the highest value of the value that meets the criteria

Status: Dp[v]: Minimum volume with a value of V

Transfer equation:

Dp[v] = min (Dp[v], Dp[v-v[i]] + p[i]);

Core:

for (i = 1; i<=n; i++)
{
for (j = sum_v; j>=v[i]; j--)
{
Dp[j] = min (Dp[j], Dp[j-v[i]]    + p[i]);
}
}

Code:

#include  <stdio.h> #include  <iostream> #include  <math.h> #include  < algorithm> #include  <string.h> #include  <string> #include  <queue> #include  <stack> #include  <map> #include  <vector> #include  <time.h> using

 namespace std;
double p[1000+10];
int v[1000+10];

double dp[5*1000+10];

	Int main () {//freopen ("a.txt",  "R",  stdin);
	int n, i, j;
		while (~SCANF ("%d",  &n)) {int sum = 0;
			for (i = 1; i<=n; i++) {scanf ("%lf%d",  &p[i], &v[i]);
		sum += v[i];
		} memset (Dp, 0x4f, sizeof (DP));
		dp[0] = 0; for (i = 1; i<=n; i++) {for (j = sum; j>=v[i]; j--) {dp[j] 
			= min (Dp[j], dp[j-v[i]] + p[i]);
}} while (dp[sum]>100) sum--;		printf ("%d\n",  sum);
} 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.