杭电2955_01背包
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955
题目大意:抢劫--n家银行,每家银行mj的钱,不被捉到的概率为pj, 在总概率不大于p的情况下, 最多抢多少钱?
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cstdlib> 6 #include <cmath> 7 #include <set> 8 #include <map> 9 #include <vector> 10 using namespace std; 11 12 int max(int a, int b) 13 { 14 return a > b ? a : b; 15 } 16 float dp[10010]; 17 int main() 18 { 19 int t, m, a[110], i, j, sum; 20 float b[110], n; 21 scanf("%d", &t); 22 while(t--) 23 { 24 sum = 0; 25 scanf("%f %d", &n, &m); 26 n = 1.0 - n; 27 for(i = 1; i <= m; i++) 28 { 29 scanf("%d %f", a + i, b + i); 30 sum += a[i]; 31 b[i] = 1 - b[i]; 32 } 33 memset(dp, 0, sizeof(dp)); 34 dp[0] = 1; 35 for(i = 1; i <= m; i++) 36 { 37 for(j = sum; j >= a[i]; j--)//从大到小, 避免这家银行抢的次数大于1 38 { 39 dp[j] = max(dp[j], dp[j - a[i]] * b[i]); 40 // cout << j << " " << dp[j] << endl; 41 } 42 } 43 for(i = sum; i > 0; i--) 44 { 45 if(dp[i] > n) 46 break; 47 } 48 printf("%d\n", i); 49 } 50 return 0; 51 }