hdu1203 dp背包问题

原来这就是背包呀,好吧没看题解我没写出,不过之前做过这种,就是求多项式的系数,这种模板还是很好用的,以后记住吧

//01背包模板
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>

using namespace std;
const int inf = (1<<31)-1;
const int MAXN = 1e4+10;

struct node{
    int val;
    double p;
}a[MAXN];

double dp[MAXN];

void init(){
    for(int i=0;i<MAXN;i++)
        dp[i] = 1.;
}

int main()
{
    int n,m;
    while(scanf("%d%d",&m,&n),n||m){
        for(int i=0;i<n;i++){
            scanf("%d%lf",&a[i].val,&a[i].p);
            a[i].p = 1-a[i].p;
        }
        init();
        for(int i=0;i<n;i++){
            for(int j=m;j>=a[i].val;j--){
                dp[j] = min(dp[j],dp[j-a[i].val]*a[i].p);
            }
        }
        printf("%.1lf%%
",100*(1-dp[m]));
    }
    //cout<<"debug"<<endl;
    //cout << "Hello world!" << endl;
    return 0;
}
//12 5 5 .5 6 .6 7 .5 2 .8 2 .7
View Code
在一个谎言的国度,沉默就是英雄
原文地址:https://www.cnblogs.com/EdsonLin/p/5383683.html