• 背包详解


    博客链接:http://www.cnblogs.com/tanky_woo/archive/2010/07/31/1789621.html

    01背包:

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2602

    代码:

     1 #include <algorithm>
     2 #include <iostream>
     3 #include <sstream>
     4 #include <cstdlib>
     5 #include <cstring>
     6 #include <cstdio>
     7 #include <string>
     8 #include <bitset>
     9 #include <vector>
    10 #include <queue>
    11 #include <stack>
    12 #include <cmath>
    13 #include <list>
    14 #include <map>
    15 #include <set>
    16 using namespace std;
    17 /***************************************/
    18 #define ll long long
    19 #define int64 __int64
    20 /***************************************/
    21 const int INF = 0x7f7f7f7f;
    22 const ll LINF = (1LL<<60);
    23 const double eps = 1e-8;
    24 const double PIE=acos(-1.0);
    25 const int d1x[]= {0,-1,0,1};
    26 const int d1y[]= {-1,0,1,0};
    27 const int d2x[]= {0,-1,0,1};
    28 const int d2y[]= {1,0,-1,0};
    29 const int fx[]= {-1,-1,-1,0,0,1,1,1};
    30 const int fy[]= {-1,0,1,-1,1,-1,0,1};
    31 inline int min_32(int (a),int (b))
    32 {
    33     return (a)<(b)?(a):(b);
    34 }
    35 inline int max_32(int (a),int (b))
    36 {
    37     return (a)>(b)?(a):(b);
    38 }
    39 inline long long min_64(long long (a),long long (b))
    40 {
    41     return (a)<(b)?(a):(b);
    42 }
    43 inline long long max_64(long long (a),long long (b))
    44 {
    45     return (a)>(b)?(a):(b);
    46 }
    47 /***************************************/
    48 void openfile()
    49 {
    50     freopen("data.in","rb",stdin);
    51     freopen("data.out","wb",stdout);
    52 }
    53 /**********************华丽丽的分割线,以上为模板部分*****************/
    54 int weight[1001],val[1001];
    55 int dp[1001];
    56 int main()
    57 {
    58     int cas;
    59     scanf("%d",&cas);
    60     while(cas--)
    61     {
    62         memset(dp,0,sizeof(dp));
    63         memset(weight,0,sizeof(weight));
    64         memset(val,0,sizeof(val));
    65         int nPack,maxVal;
    66         int i,j;
    67         scanf("%d%d",&nPack,&maxVal);
    68         for(i=0;i<nPack;i++)
    69         {
    70             scanf("%d",&val[i]);
    71         }
    72         for(i=0;i<nPack;i++)
    73         {
    74             scanf("%d",&weight[i]);
    75         }
    76         for(i=0;i<nPack;i++)
    77             for(j=maxVal;j>=weight[i];j--)
    78             {
    79                 if (dp[j-weight[i]]+val[i]>dp[j])
    80                     dp[j]=dp[j-weight[i]]+val[i];
    81             }
    82         printf("%d
    ",dp[maxVal]);
    83     }
    84     return 0;
    85 }
    View Code

    完全背包:

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1114

    代码:

     1 #include <algorithm>
     2 #include <iostream>
     3 #include <sstream>
     4 #include <cstdlib>
     5 #include <cstring>
     6 #include <cstdio>
     7 #include <string>
     8 #include <bitset>
     9 #include <vector>
    10 #include <queue>
    11 #include <stack>
    12 #include <cmath>
    13 #include <list>
    14 #include <map>
    15 #include <set>
    16 using namespace std;
    17 /***************************************/
    18 #define ll long long
    19 #define int64 __int64
    20 /***************************************/
    21 const int INF = 0x7f7f7f7f;
    22 const ll LINF = (1LL<<60);
    23 const double eps = 1e-8;
    24 const double PIE=acos(-1.0);
    25 const int d1x[]= {0,-1,0,1};
    26 const int d1y[]= {-1,0,1,0};
    27 const int d2x[]= {0,-1,0,1};
    28 const int d2y[]= {1,0,-1,0};
    29 const int fx[]= {-1,-1,-1,0,0,1,1,1};
    30 const int fy[]= {-1,0,1,-1,1,-1,0,1};
    31 inline int min_32(int (a),int (b))
    32 {
    33     return (a)<(b)?(a):(b);
    34 }
    35 inline int max_32(int (a),int (b))
    36 {
    37     return (a)>(b)?(a):(b);
    38 }
    39 inline long long min_64(long long (a),long long (b))
    40 {
    41     return (a)<(b)?(a):(b);
    42 }
    43 inline long long max_64(long long (a),long long (b))
    44 {
    45     return (a)>(b)?(a):(b);
    46 }
    47 /***************************************/
    48 void openfile()
    49 {
    50     freopen("data.in","rb",stdin);
    51     freopen("data.out","wb",stdout);
    52 }
    53 /**********************华丽丽的分割线,以上为模板部分*****************/
    54 int weight[10001],val[50001];
    55 int dp[10001];
    56 int main()
    57 {
    58     int cas;
    59     scanf("%d",&cas);
    60     while(cas--)
    61     {
    62         memset(dp,0,sizeof(dp));
    63         memset(weight,0,sizeof(weight));
    64         memset(val,0,sizeof(val));
    65         int nPack,Val;
    66         int i,j;
    67         int Eval,Pval;
    68         scanf("%d%d",&Eval,&Pval);
    69         Val=Pval-Eval;
    70         scanf("%d",&nPack);
    71         for(i=0;i<nPack;i++)
    72         {
    73             scanf("%d%d",&val[i],&weight[i]);
    74         }
    75         for(i=0;i<=Val;i++)
    76             dp[i]=INF;
    77         dp[0]=0;
    78         for(i=0;i<nPack;i++)
    79             for(j=weight[i];j<=Val;j++)
    80             {
    81                 if (dp[j-weight[i]]+val[i]<dp[j])
    82                     dp[j]=dp[j-weight[i]]+val[i];
    83             }
    84         if (dp[Val]==INF)
    85             printf("This is impossible.
    ");
    86         else
    87             printf("The minimum amount of money in the piggy-bank is %d.
    ",dp[Val]);
    88     }
    89     return 0;
    90 }
    View Code

    多重背包:

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2191

    代码:

     1 #include <algorithm>
     2 #include <iostream>
     3 #include <sstream>
     4 #include <cstdlib>
     5 #include <cstring>
     6 #include <cstdio>
     7 #include <string>
     8 #include <bitset>
     9 #include <vector>
    10 #include <queue>
    11 #include <stack>
    12 #include <cmath>
    13 #include <list>
    14 #include <map>
    15 #include <set>
    16 using namespace std;
    17 /***************************************/
    18 #define ll long long
    19 #define int64 __int64
    20 /***************************************/
    21 const int INF = 0x7f7f7f7f;
    22 const ll LINF = (1LL<<60);
    23 const double eps = 1e-8;
    24 const double PIE=acos(-1.0);
    25 const int d1x[]= {0,-1,0,1};
    26 const int d1y[]= {-1,0,1,0};
    27 const int d2x[]= {0,-1,0,1};
    28 const int d2y[]= {1,0,-1,0};
    29 const int fx[]= {-1,-1,-1,0,0,1,1,1};
    30 const int fy[]= {-1,0,1,-1,1,-1,0,1};
    31 inline int min_32(int (a),int (b))
    32 {
    33     return (a)<(b)?(a):(b);
    34 }
    35 inline int max_32(int (a),int (b))
    36 {
    37     return (a)>(b)?(a):(b);
    38 }
    39 inline long long min_64(long long (a),long long (b))
    40 {
    41     return (a)<(b)?(a):(b);
    42 }
    43 inline long long max_64(long long (a),long long (b))
    44 {
    45     return (a)>(b)?(a):(b);
    46 }
    47 /***************************************/
    48 void openfile()
    49 {
    50     freopen("data.in","rb",stdin);
    51     freopen("data.out","wb",stdout);
    52 }
    53 /**********************华丽丽的分割线,以上为模板部分*****************/
    54 int weight[105],val[105],num[105];
    55 int dp[105];
    56 int main()
    57 {
    58     int cas;
    59     scanf("%d",&cas);
    60     while(cas--)
    61     {
    62         memset(dp,0,sizeof(dp));
    63         memset(weight,0,sizeof(weight));
    64         memset(val,0,sizeof(val));
    65         memset(num,0,sizeof(num));
    66         int nPack,Val;
    67         int i,j,k;
    68         scanf("%d%d",&Val,&nPack);
    69         for(i=0;i<nPack;i++)
    70         {
    71             scanf("%d%d%d",&weight[i],&val[i],&num[i]);
    72         }
    73         for(i=0;i<nPack;i++)
    74             for(k=0;k<num[i];k++)
    75                 for(j=Val;j>=weight[i];j--)
    76                     if(dp[j-weight[i]]+val[i]>dp[j])
    77                         dp[j]=dp[j-weight[i]]+val[i];
    78         printf("%d
    ",dp[Val]);
    79     }
    80     return 0;
    81 }
    82 
    83 /*
    84 
    85 1
    86 8 2
    87 2 100 4
    88 4 100 2
    89 
    90 
    91 1
    92 10 2
    93 2 100 4
    94 4 100 2
    95 
    96 */
    View Code
  • 相关阅读:
    MySQL MHA高可用方案
    微服务架构的基础框架选择:Spring Cloud还是Dubbo?
    elasticsearch
    spring Cloud构建微服务架构
    SpringBoot-Learning
    JetBrains激活
    正则表达式(java)规则大全
    Android调用Webservice
    UCML针对数据表的修改自定义维护数据变更记录
    SQL游标模板
  • 原文地址:https://www.cnblogs.com/ZhaoPengkinghold/p/4172060.html
Copyright © 2020-2023  润新知