首页 > 代码库 > hdu3496+poj1948(二维费用背包)

hdu3496+poj1948(二维费用背包)

Watch The Movie

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 5106    Accepted Submission(s): 1614


Problem Description
New semester is coming, and DuoDuo has to go to school tomorrow. She decides to have fun tonight and will be very busy after tonight. She like watch cartoon very much. So she wants her uncle to buy some movies and watch with her tonight. Her grandfather gave them L minutes to watch the cartoon. After that they have to go to sleep.
DuoDuo list N piece of movies from 1 to N. All of them are her favorite, and she wants her uncle buy for her. She give a value Vi (Vi > 0) of the N piece of movies. The higher value a movie gets shows that DuoDuo likes it more. Each movie has a time Ti to play over. If a movie DuoDuo choice to watch she won’t stop until it goes to end.
But there is a strange problem, the shop just sell M piece of movies (not less or more then), It is difficult for her uncle to make the decision. How to select M piece of movies from N piece of DVDs that DuoDuo want to get the highest value and the time they cost not more then L.
How clever you are! Please help DuoDuo’s uncle.
 

Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case:
The first line is: N(N <= 100),M(M<=N),L(L <= 1000)
N: the number of DVD that DuoDuo want buy.
M: the number of DVD that the shop can sale.
L: the longest time that her grandfather allowed to watch.
The second line to N+1 line, each line contain two numbers. The first number is the time of the ith DVD, and the second number is the value of ith DVD that DuoDuo rated.
 

Output
Contain one number. (It is less then 2^31.)
The total value that DuoDuo can get tonight.
If DuoDuo can’t watch all of the movies that her uncle had bought for her, please output 0.
 

Sample Input
1 3 2 10 11 100 1 2 9 1
 

Sample Output
3
 

Source
2010 ACM-ICPC Multi-University Training Contest(6)——Host by BIT
题解:
     典型的二维费用背包,增加一维费用皆可,条件抽象为01背包在其中一维费用装满情况下,直接贴地推公的:      
                 dp[j][k]=max(dp[j][k],dp[j-g[i].cost][k-1]+g[i].val);            

          直接贴代码:

#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN=100+10;
struct Node
{
	int cost,val;
}g[MAXN];
int n,m,l;
int dp[1000+10][MAXN];

int solve()
{
	memset(dp,-1,sizeof(dp));
	for(int j=l;j>=0;j--)
		dp[j][0]=0;

	for(int i=0;i<n;i++)
	{
		for(int j=l;j>=g[i].cost;j--)
		{
			for(int k=m;k>=1;k--)
			{
				if(dp[j-g[i].cost][k-1]>=0)
				  dp[j][k]=max(dp[j][k],dp[j-g[i].cost][k-1]+g[i].val);
			}
		}
	}
	if(dp[l][m]==-1)
	  return 0;
	return dp[l][m];
}

int main()
{
	int cas;
	cin>>cas;
	while(cas--)
	{
		scanf("%d%d%d",&n,&m,&l);
		for(int i=0;i<n;i++)
			scanf("%d%d",&g[i].cost,&g[i].val);

		printf("%d\n",solve());
	}
	system("pause");
	return 0;
}

      还有一道可以用二维背包求解的问题poj1948。
Triangular Pastures

Time Limit: 1000MS  Memory Limit: 30000K
Total Submissions: 6348  Accepted: 2057

Description

Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.
I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N (3 <= N <= 40) fence segments (each of integer length Li (1 <= Li <= 40) and must arrange them into a triangular pasture with the largest grazing area. Ms. Hei must use all the rails to create three sides of non-zero length.
Help Ms. Hei convince the rest of the herd that plenty of grazing land will be available.Calculate the largest area that may be enclosed with a supplied set of fence segments.


Input
* Line 1: A single integer N
* Lines 2..N+1: N lines, each with a single integer representing one fence segment‘s length. The lengths are not necessarily unique.

Output
A single line with the integer that is the truncated integer representation of the largest possible enclosed area multiplied by 100. Output -1 if no triangle of positive area may be constructed.

Sample Input
5
1
1
3
3
4

Sample Output
692

Hint
[which is 100x the area of an equilateral triangle with side length 4]

题解:
     给定n条线段求能围城三角形的最大值。本题
应该枚举3条边长,然后判断能否构成三角形。枚举三角形边长可以转换为01背包,dp[i][j]表示构成长度为i,j的两条边,dp[i][j]=1表示存在,否则不存在。即转换成在装满情况下的二维费用01背包,若直接转换成三维可能内存不够,然后判断能否构成三角形从而选择最大者。
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;

const int MAXN=40+5;
int n,maxLen,sum;
int dp[MAXN*MAXN/2][MAXN*MAXN/2];
int seg[MAXN];

int solve()
{
	memset(dp,0,sizeof(dp));
	maxLen=sum/2;
	dp[0][0]=1;
	for(int i=0;i<n;i++)
	{
		for(int j=maxLen;j>=0;j--)
		{
			for(int k=maxLen;k>=0;k--)
			{
				if((j>=seg[i]&&dp[j-seg[i]][k])||(k>=seg[i]&&dp[j][k-seg[i]]))
					dp[j][k]=1;
			}
		}
	}

	int ans=-1;
	for(int i=0;i<=maxLen;i++)
	{
		for(int j=0;j<=maxLen;j++)
		{
			int temp=sum-i-j;
			if(dp[i][j]&&i+j>temp&&i+temp>j&&j+temp>i)
			{
				double p=1.0*sum/2;
				int area=(int)(sqrt(p*(p-i)*(p-temp)*(p-j))*100);
				if(area>ans)
					ans=area;
			}
		}
	}

	return ans;
}

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		sum=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&seg[i]);
			sum+=seg[i];
		}
		printf("%d\n",solve());
	}
	system("pause");
	return 0;
}