• 大0-1背包


    Beautiful Land

    #include<cstdio>价值背包
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<map>
    #define max(a,b)(a>b?a:b)
    #define min(a,b)(a<b?a:b)
    typedef long long ll;
    using namespace std;
    #define N 10005
    ll dp[N],w[N];  ///此时的dp[i]表示的是;价值为i时的最小容量为dp[i];
    int v[N];
      
    int main()
    {
        int T,i,n,sum;
        ll V;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%I64d",&n,&V);
            sum=0;
            for(i=1;i<=n;i++)
            {
                scanf("%I64d%d",&w[i],&v[i]);
                sum=sum+v[i];
            }
      
            memset(dp,1000000010,sizeof(dp)); ///要求最小容量,初始化为最大值;
            dp[0]=0;
            for(i=1;i<=n;i++)
            {
                for(int j=sum;j>=v[i];j--)
                    dp[j]=min(dp[j],dp[j-v[i]]+w[i]);
            }
      
            for(i=sum;i>=0;i--)
            {
                if(dp[i]<=V)
                {
                   printf("%d
    ",i); ///此处输出i,即为满足条件的最大价值
                   break;
                }
            }
        }
        return 0;
    }
    View Code

     

    卡迪亚的游戏

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <list>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <iomanip>
    #define ull unsigned long long
    #define ll long long
    #define pb push_back
    #define mem(sum,x) memset(sum,x,sizeof(sum))
    #define rep(i,start,end) for(int i=start;i<=end;i++)
    #define per(i,end,start) for(int i=end;i>=start;i--)
    #define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    using namespace std;
    const int mod = 998244353;
    const int mxn = 2e4 +7;
    int _,n,m,k,t,u,ans,cnt,ok,lim;
    ll w[mxn] , cost[mxn] , dp[mxn];
    #define lc now<<1
    #define rc now<<1|1
    bool vis[mxn];
    int main()
    {
        ll lim , cnt = 1 , ans = 0  ;
        scanf("%d %lld",&n,&lim);
        for(int i=1;i<=n;i++)
            scanf("%lld %lld",&cost[i],&w[i]) , ans += w[i];
        for(int i=0;i<=ans;i++)
            dp[i] = mod ;
        dp[0] = 0 ;
        for(int i=1;i<=n;i++)
        {
            for(int j=ans;j>=w[i];j--)
            {
                dp[j] = min(dp[j] , dp[j- w[i] ]+cost[i]);
            }
        }
        for(int i=ans;i>=0;i--)
        {
            if(dp[i]<=lim)
            {
                printf("%d
    ",i);
                break;
            }
        }
    
    }
    View Code

    Knapsack problem

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <list>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <iomanip>
    #define ull unsigned long long
    #define ll long long
    #define pb push_back
    #define mem(sum,x) memset(sum,x,sizeof(sum))
    #define rep(i,start,end) for(int i=start;i<=end;i++)
    #define per(i,end,start) for(int i=end;i>=start;i--)
    #define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    using namespace std;
    const int mod = 998244353;
    const int mxn = 2e4 +7;
    int _,n,m,t,u,ans,cnt,ok,lim;
    ll w[mxn] , cost[mxn] , dp[mxn] , far[mxn] , siz[mxn];
    double num[mxn];
    #define lc now<<1
    #define rc now<<1|1
    int main()
    {
        for(cin>>t;t;t--)
        {
            cin>>n>>m;
            ll ans = 0 ;dp[0] = 0 ;
            rep(i,1,n)
                cin>>cost[i]>>w[i] , ans+=w[i] ;
            for(int i=1;i<=ans;i++)  dp[i] = mod ;
            for(int i=1;i<=n;i++)
            {
                for(int j=ans;j>=w[i];j--)
                    dp[j] = min(dp[j],dp[ j-w[i] ]+cost[i]);
            }
            for(int i=ans;i>=0;i--)
            {
                if(dp[i]<=m)
                {
                    cout<<i<<endl;
                    break;
                }
            }
        }
    }
    View Code
    所遇皆星河
  • 相关阅读:
    C语言II作业01
    C语言寒假大作战04
    C语言寒假大作战03
    C语言寒假大作战02
    C语言寒假大作战01
    C语言ll作业01
    C语言寒假大作战04
    C语言寒假大作战03
    C语言寒假大作战02
    C语言寒假大作战01
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/12823128.html
Copyright © 2020-2023  润新知