首页 > ACM题库 > HDU-杭电 > HDU 3033-I love sneakers!-背包问题-[解题报告]HOJ
2014
02-27

HDU 3033-I love sneakers!-背包问题-[解题报告]HOJ

I love sneakers!

问题描述 :

After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store.
Nim or not Nim?

There are several brands of sneakers that Iserlohn wants to collect, such as Air Jordan and Nike Pro. And each brand has released various products. For the reason that Iserlohn is definitely a sneaker-mania, he desires to buy at least one product for each brand.
Although the fixed price of each product has been labeled, Iserlohn sets values for each of them based on his own tendency. With handsome but limited money, he wants to maximize the total value of the shoes he is going to buy. Obviously, as a collector, he won’t buy the same product twice.
Now, Iserlohn needs you to help him find the best solution of his problem, which means to maximize the total value of the products he can buy.

输入:

Input contains multiple test cases. Each test case begins with three integers 1<=N<=100 representing the total number of products, 1 <= M<= 10000 the money Iserlohn gets, and 1<=K<=10 representing the sneaker brands. The following N lines each represents a product with three positive integers 1<=a<=k, b and c, 0<=b,c<100000, meaning the brand’s number it belongs, the labeled price, and the value of this product. Process to End Of File.

输出:

Input contains multiple test cases. Each test case begins with three integers 1<=N<=100 representing the total number of products, 1 <= M<= 10000 the money Iserlohn gets, and 1<=K<=10 representing the sneaker brands. The following N lines each represents a product with three positive integers 1<=a<=k, b and c, 0<=b,c<100000, meaning the brand’s number it belongs, the labeled price, and the value of this product. Process to End Of File.

样例输入:

5 10000 3
1 4 6
2 5 7
3 4 99
1 55 77
2 44 66

样例输出:

255

#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <iostream>
#define max2(a,b) ((a) > (b) ? (a) : (b))
#define min2(a,b) ((a) < (b) ? (a) : (b))
using namespace std;

typedef struct node
{
    int v,w;
} shoe;
vector<shoe>brand[15];
int main()
{
    int dp[15][10005];
    int n,m,k,a;
    shoe s;
    while(cin>>n>>m>>k)
    {
        for(int i=0;i<=k;i++)
        brand[i].clear();
        for(int i=0; i<n; i++)
        {
            cin>>a>>s.v>>s.w;
            brand[a].push_back(s);
        }
        memset(dp,-1,sizeof(dp));
        memset(dp[0],0,sizeof(dp[0]));

        for(int i=1;i<=k;i++)
        for(int p=0;p<(int)brand[i].size();p++)
        for(int j=m;j>=brand[i][p].v;j--)
        {
            if(dp[i][j-brand[i][p].v]!=-1) dp[i][j]=max2(dp[i][j],dp[i][j-brand[i][p].v]+brand[i][p].w); //同一种类的鞋子能取多个
            if(dp[i-1][j-brand[i][p].v]!=-1) dp[i][j]=max2(dp[i][j],dp[i-1][j-brand[i][p].v]+brand[i][p].w);
        }
        if(dp[k][m]>=0)
        cout<<dp[k][m]<<endl;
        else
        cout<<"Impossible"<<endl;
    }
    return 0;
}

参考:http://blog.csdn.net/weyuli/article/details/9258659


  1. 漂亮。佩服。
    P.S. unsigned 应该去掉。换行符是n 不是/n
    还可以稍微优化一下,
    int main() {
    int m,n,ai,aj,bi,bj,ak,bk;
    while (scanf("%d%d",&m,&n)!=EOF) {
    ai = sqrt(m-1);
    bi = sqrt(n-1);
    aj = (m-ai*ai-1)>>1;
    bj = (n-bi*bi-1)>>1;
    ak = ((ai+1)*(ai+1)-m)>>1;
    bk = ((bi+1)*(bi+1)-n)>>1;
    printf("%dn",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
    }
    }