zoukankan      html  css  js  c++  java
  • HDU 5501 背包问题

    需要按照B/C的值从大到小排序。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<map>
    using namespace std;
    typedef long long LL;
    const int INF = 1e9+7;
    const int maxn = 1015;
    const int MOD = 9973;
    
    int A[maxn], B[maxn], C[maxn];
    int dp[3005];
    int vis[maxn];
    
    struct node
    {
        int A, B, C;
        bool friend operator < (node a, node b)
        {
            return 1.0*a.B/a.C > 1.0*b.B/b.C;
        }
    }P[maxn];
    
    
    int main()
    {
        int T;
        scanf("%d",  &T);
    
        while(T--)
        {
            int n, t;
            scanf("%d %d", &n, &t);
            for(int i=1; i<=n; i++)
                scanf("%d %d %d", &P[i].A, &P[i].B, &P[i].C);
    
            sort(P+1, P+n+1);
            memset(dp, 0, sizeof(dp));
    
            for(int i=1; i<= n; i++)
                vis[i] = INF;
    
            for(int i=1; i<=n; i++)
            {
                for(int j=t; j>=P[i].C; j--)
                    dp[j] = max(dp[j], dp[j-P[i].C] + P[i].A - P[i].B*j);
            }
    
            int ans = 0;
            for(int i=0; i<= t; i++)
                ans = max(ans, dp[i]);
    
            printf("%d
    ", ans);
        }
        return 0;
    }
    /*
    1
    2 10
    110 5 9
    30 2 1
    */
  • 相关阅读:
    u Calculate e
    Elevator
    骑士走棋盘
    Number Sequence
    老鼠走迷宫
    Let the Balloon Rise
    A+B Problem II
    Three-Color Flag
    Noldbach problem
    Almost Prime
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/4871912.html
Copyright © 2011-2022 走看看