题目链接:https://vjudge.net/problem/LightOJ-1079
需要注意的是,要想连续抢多家银行就得保证抢前面的银行没有被抓,所以样例1中抢1和2两家银行的概率是 0.02 + (1-0.02)*0.03;
这里的背包dp[i] 中的i是val,价值;
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
const int Maxn = 110;
double dp[10005];
int val[105];
double p[105];
int main (void)
{
int T;
cin >> T;
for (int cas = 1; cas <= T; ++cas) {
for (int i = 0; i < 10005; ++i) dp[i] = 1;
int m; double limit;
cin >> limit >> m;
for (int i = 1; i <= m; ++i) {
cin >> val[i] >> p[i];
}
dp[0] = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 10000-val[i]; j >= 0; --j) {
if (dp[j] == 1) continue;
dp[j+val[i]] = min (dp[j+val[i]], dp[j]+(1-dp[j])*p[i]);
}
}
for (int i = 10000; i >= 0; --i) {
if (dp[i] < limit || (fabs (limit-dp[i]) < 1e-6)) {
cout << "Case " << cas << ": " << i << endl;
break;
}
}
}
return 0;
}