【luogu】 洛谷普及练习场 动态规划的背包问题[动态规划]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define rg register 4 #define ll long long 5 const int N=30,C=30000+5; 6 int n,m,ans=0,c[N],a[N],f[C]; 7 template<class t>void rd(t &x) 8 { 9 x=0;int w=0;char ch=0; 10 while(!isdigit(ch)) w|=ch=='-',ch=getchar(); 11 while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); 12 x=w?-x:x; 13 } 14 15 int main() 16 { 17 rd(n),rd(m); 18 memset(f,0,sizeof(f)); 19 for(rg int i=1;i<=m;++i) rd(c[i]),rd(a[i]); 20 for(rg int i=1;i<=m;++i) 21 for(rg int j=n;j>=c[i];--j) 22 f[j]=max(f[j],f[j-c[i]]+c[i]*a[i]); 23 for(rg int i=0;i<=n;++i) ans=max(ans,f[i]); 24 printf("%d",ans); 25 return 0; 26 } 27 28 P1060 开心的金明
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define rg register 4 #define ll long long 5 const int N=100+5,M=10000+5; 6 int n,m,a[N],f[M]; 7 template<class t>void rd(t &x) 8 { 9 x=0;int w=0;char ch=0; 10 while(!isdigit(ch)) w|=ch=='-',ch=getchar(); 11 while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); 12 x=w?-x:x; 13 } 14 15 int main() 16 { 17 rd(n),rd(m); 18 memset(f,0,sizeof(f));f[0]=1; 19 for(rg int i=1;i<=n;++i) rd(a[i]); 20 for(rg int i=1;i<=n;++i) 21 for(rg int j=m;j>=a[i];--j) 22 f[j]+=f[j-a[i]]; 23 printf("%d",f[m]); 24 return 0; 25 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define rg register 4 #define ll long long 5 const int N=10000+5,T=100000+5; 6 int t,n,c[N],a[N],f[T]; 7 template<class t>void rd(t &x) 8 { 9 x=0;int w=0;char ch=0; 10 while(!isdigit(ch)) w|=ch=='-',ch=getchar(); 11 while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); 12 x=w?-x:x; 13 } 14 15 int main() 16 { 17 rd(t),rd(n); 18 memset(f,0,sizeof(f)); 19 for(rg int i=1;i<=n;++i) rd(c[i]),rd(a[i]); 20 for(rg int i=1;i<=t;++i) 21 for(rg int j=1;j<=n;++j) 22 if(i-c[j]>=0) 23 f[i]=max(f[i],f[i-c[j]]+a[j]); 24 printf("%d",f[t]); 25 return 0; 26 } 27 28 P1616 疯狂的采药
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define rg register 4 #define ll long long 5 const int N=32000+5,M=60+5; 6 int n,mm,ans=0,f[N],c[M][M],w[M][M]; 7 template<class t>void rd(t &x) 8 { 9 x=0;int w=0;char ch=0; 10 while(!isdigit(ch)) w|=ch=='-',ch=getchar(); 11 while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); 12 x=w?-x:x; 13 } 14 15 int main() 16 { 17 rd(n),rd(mm); 18 memset(f,0,sizeof(f)); 19 memset(c,0,sizeof(c)); 20 memset(w,0,sizeof(w)); 21 for(rg int i=1;i<=mm;++i) 22 { 23 int v,p,q; 24 rd(v),rd(p),rd(q); 25 if(!q) c[i][0]=v,w[i][0]=v*p; 26 else 27 { 28 int cur=1; 29 while(c[q][cur]) ++cur; 30 c[q][cur]=v,w[q][cur]=v*p; 31 } 32 } 33 for(rg int i=1;i<=mm;++i) 34 { 35 if(c[i][0]) 36 { 37 for(rg int j=n;j>=c[i][0];--j) 38 { 39 f[j]=max(f[j],f[j-c[i][0]]+w[i][0]); 40 if(j>=c[i][0]+c[i][1]) f[j]=max(f[j],f[j-c[i][0]-c[i][1]]+w[i][0]+w[i][1]); 41 if(j>=c[i][0]+c[i][2]) f[j]=max(f[j],f[j-c[i][0]-c[i][2]]+w[i][0]+w[i][2]); 42 if(j>=c[i][0]+c[i][1]+c[i][2]) f[j]=max(f[j],f[j-c[i][0]-c[i][1]-c[i][2]]+w[i][0]+w[i][1]+w[i][2]); 43 } 44 } 45 } 46 for(int i=1;i<=n;++i) 47 ans=max(ans,f[i]); 48 printf("%d",ans); 49 return 0; 50 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 id:gww 3 language: 4 啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 5 */ 6 #include<bits/stdc++.h> 7 using namespace std; 8 const int N=100+10; 9 int t,n,k[N],v[N],f[1000+10]; 10 int rd() 11 { 12 int x=0,w=0;char ch=0; 13 while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} 14 while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); 15 return w?-x:x; 16 } 17 18 int main() 19 { 20 t=rd(),n=rd(); 21 for(int i=1;i<=n;i++) 22 v[i]=rd(),k[i]=rd(); 23 for(int i=1;i<=n;i++) 24 for(int j=t;j>=v[i];j--) 25 f[j]=max(f[j-v[i]]+k[i],f[j]); 26 printf("%d",f[t]); 27 return 0; 28 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[35],f[35][20005]; 4 int main() 5 { 6 int v,n; 7 scanf("%d%d",&v,&n); 8 for(int i=1;i<=n;i++) 9 scanf("%d",&a[i]); 10 memset(f,0,sizeof(f)); 11 f[0][0]=1; 12 for(int i=1;i<=n;i++) 13 for(int j=0;j<=v;j++) 14 { 15 if(f[i-1][j]) f[i][j]=1; 16 if(j>=a[i]&&f[i-1][j-a[i]]) f[i][j]=1; 17 } 18 int ans=0; 19 for(int i=0;i<=v;i++) 20 if(f[n][i]) ans=i; 21 printf("%d",v-ans); 22 return 0; 23 }