题目链接:
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.
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.题意描述:有n个物品,多种品牌,每个产品有三个属性a,b和c,分别代表品牌种类、价钱和价值。现在你有现金m和k个伙伴,要求为每一位伙伴至少买一件物品,求出得到的最大价值(编号为u的伙伴只能得到品牌种类为u的物品)。
算法分析:分组背包。dp[i][j]代表前 i 种物品、身上有j元获得的最大价值。
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #define inf 0x7fffffff10 using namespace std;11 const int maxn=100+10;12 13 int n,m,k;14 int dp[12][10000+10];15 vector > vec[maxn];16 17 int main()18 {19 while (scanf("%d%d%d",&n,&m,&k)!=EOF)20 {21 int a,b,c;22 for (int i=1 ;i<=n ;i++) vec[i].clear();23 for (int i=1 ;i<=n ;i++)24 {25 scanf("%d%d%d",&a,&b,&c);26 vec[a].push_back(make_pair(b,c));27 }28 memset(dp,-1,sizeof(dp));29 memset(dp[0],0,sizeof(dp[0]));30 int flag=0;31 // for (int i=1 ;i<=k ;i++)32 // {33 // int num=vec[i].size();34 // for (int j=0 ;j =p ;j--)46 {47 48 int value=vec[i][u].second;49 if (dp[i][j-p]!=-1)50 dp[i][j]=max(dp[i][j],dp[i][j-p]+value);51 if (dp[i-1][j-p]!=-1)52 dp[i][j]=max(dp[i][j],dp[i-1][j-p]+value);53 }54 }55 }56 if (flag || dp[k][m]==-1) printf("Impossible\n");57 else printf("%d\n",dp[k][m]);58 }59 return 0;60 }