啊哈磊_编程从这里起步

标题: [dp]不是采药 [打印本页]

作者: 邀请码    时间: 2016-8-23 15:52
标题: [dp]不是采药

  1. #include <stdio.h>
  2. int max(int x,int y){return x>y?x:y;}
  3. int main(){
  4. &nbsp; &nbsp; int n,m,w[101],c[101],dp[1001]={0};
  5. &nbsp; &nbsp; scanf("%d%d",&m,&n);
  6. &nbsp; &nbsp; for(int i=1;i<=n;i++)
  7. &nbsp; &nbsp; scanf("%d%d",c+i,w+i);
  8. &nbsp; &nbsp; for(int i=1;i<=n;i++)
  9. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;for(int j=c[i];j<=n;j++)
  10. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;dp[j]=max(dp[j],dp[j-c[i]]+w[i]);
  11. &nbsp; &nbsp; printf("%d\n",dp[m]);
  12. &nbsp; &nbsp; return 0;
  13. }
复制代码

作者: 邀请码    时间: 2016-8-23 15:53
卧槽哪里来的那么多&nbsp;
作者: 邀请码    时间: 2016-8-23 15:54


  1. #include <stdio.h>
  2. int max(int x,int y){return x>y?x:y;}
  3. int main(){
  4.     int n,m,w[101],c[101],dp[1001]={0};
  5.     scanf("%d%d",&m,&n);
  6.     for(int i=1;i<=n;i++)
  7.     scanf("%d%d",c+i,w+i);
  8.     for(int i=1;i<=n;i++)
  9.         for(int j=c[i];j<=n;j++)
  10.             dp[j]=max(dp[j],dp[j-c[i]]+w[i]);
  11.     printf("%d\n",dp[m]);
  12.     return 0;
  13. }
复制代码





欢迎光临 啊哈磊_编程从这里起步 (https://bbs.codeaha.com/) Powered by Discuz! X3.2