跑背包,求每个体力可以搞的体积,然后就没了.
1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int n,c,v,w[10001],a[10001],f[10001]; 7 8 int main() { 9 scanf("%d%d%d",&v,&n,&c); 10 for(int i = 1;i <= n; i++) 11 scanf("%d%d",&w[i],&a[i]); 12 for(int i = 1;i <= n; i++) 13 for(int j = c;j >= a[i]; j--) 14 f[j] = max(f[j],f[j-a[i]] + w[i]); 15 for(int i = 0;i <= c; i++) 16 if(f[i] >= v) { 17 printf("%d",c - i); 18 return 0; 19 } 20 printf("Impossible"); 21 return 0; 22 }
原文:https://www.cnblogs.com/lipeiyi520/p/12293384.html