P1510 精卫填海(01背包)

#include<bits/stdc++.h>

using namespace std;
const int maxn = 1e4 + 10;
int n,c,v,vv[maxn],w[maxn],dp[maxn];
int main(){
    //freopen("in","r",stdin);
    ios::sync_with_stdio(0);
    cin>> v >> n >> c;
    for(int i=1;i<=n;i++)
        cin>> w[i] >> vv[i];
    for(int i=1;i<=n;i++){
        for(int j=c;j>=vv[i];j--){
            dp[j] = max(dp[j],dp[j - vv[i]] + w[i]);
        }
    }
    for(int i = 1; i <= c; i++){
        if(dp[i] >= v){
            cout << c - i;
            return 0;
        }
    }
    cout<<"Impossible";
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/xcfxcf/p/12814199.html