hdoj练习题上几道需要思考的背包题

1.Piggy-Bank

Problem Description

Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.

But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it’s weight in grams.

Output

Print exactly one line of output for each test case. The line must contain the sentence “The minimum amount of money in the piggy-bank is X.” where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line “This is impossible.”.

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
13
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

Sample Output

1
2
3
The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.

Source

Central Europe 1999\

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
#include<algorithm>
#include<string.h>

using namespace std;

const int N = 100010;
const int INT_M = 5000000;

int t;

int n, m;
int v[N], w[N];
int f[N];

int main()
{
cin >> t;
while (t--)
{
int pig_weight, all_weight;
cin >> pig_weight >> all_weight;
cin >> n;
m = all_weight - pig_weight;
for(int i = 1 ; i <= n ; i++)
{
cin >> w[i] >> v[i];
}
//memset(f, INT_M, sizeof(f));
for (int i = 0 ; i <= m ; i++) f[i] = INT_M;

f[0] = 0;
for (int i = 1 ; i <= n ; i++)
{
for (int j = v[i] ; j <= m ; j++)
{
f[j] = min(f[j], f[j - v[i]] + w[i]);
}
}
if(f[m] == INT_M)
{
cout << "This is impossible." << endl;
}
else
{
cout << "The minimum amount of money in the piggy-bank is " << f[m] << "." <<endl;
}

}
return 0;
}

这道题需要注意的是这是典型的求最小值的背包,在初始化dp数组是要将其初始化成一个很大的值

2.I Need OFFER!

Problem Description

Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了。要申请国外的任何大学,你都要交纳一定的申请费用,这可是很惊人的。Speakless没有多少钱,总共只攒了n万美元。他将在m个学校中选择若干的(当然要在他的经济承受范围内)。每个学校都有不同的申请费用a(万美元),并且Speakless估计了他得到这个学校offer的可能性b。不同学校之间是否得到offer不会互相影响。“I NEED A OFFER”,他大叫一声。帮帮这个可怜的人吧,帮助他计算一下,他可以收到至少一份offer的最大概率。(如果Speakless选择了多个学校,得到任意一个学校的offer都可以)。

Input

输入有若干组数据,每组数据的第一行有两个正整数n,m(0<=n<=10000,0<=m<=10000) 后面的m行,每行都有两个数据ai(整型),bi(实型)分别表示第i个学校的申请费用和可能拿到offer的概率。 输入的最后有两个0。

Output

每组数据都对应一个输出,表示Speakless可能得到至少一份offer的最大概率。用百分数表示,精确到小数点后一位。

Sample Input

1
2
3
4
5
>10 3
>4 0.1
>4 0.2
>5 0.3
>0 0

这道题要好好注意状态转移方程,还需要注意dp数组的初始化为1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;

const int N = 10010;

int n, m;
int v[N];
double w[N], f[N];


int main()
{
while(cin >> m >> n)
{
if(!n && !m) break;

memset(f, 0, sizeof(f));

for(int i = 1; i <= n ; i++)
{
cin >> v[i] >> w[i];
w[i] = 1 - w[i];
}

for(int i = 0 ; i <= m ; i++) f[i] = 1.0;

for(int i = 1 ; i <= n ; i++)
{
for(int j = m ; j >= v[i] ; j--)
{
f[j] = min(f[j], f[j - v[i]] * w[i]);
}
}

printf("%.1f%%\n",100*(1.0-f[m]));

}
}