码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1293 Duty Free Shop

时间:2019-08-14 20:01:11      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:font   desc   sed   gif   each   容量   ring   signed   lin   

Duty Free Shop
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1896   Accepted: 730   Special Judge

Description

Pedro travelled to Europe to take part in the International Olympiad in Informatics and is coming back home. Since all his friends asked him to bring them some gift, he bought two big bags of chocolates (one of Mindt and one of Lilka). Each of these two bags contains a certain number of small chocolates. Buying those two bags was much less expensive than buying smaller, individual boxes of chocolates. At home, Pedro has some empty chocolate boxes that he kept from other trips. Pedro intends to distribute the chocolates he just bought into these smaller boxes, to give them to his friends. 
As soon as Pedro begins filling the small boxes, he realizes he has a big problem: since he has two different brands of chocolates, if he mixes chocolates of different brands into one small box, the friend who receives this small box will discover Pedro‘s trick to save money, and will not be pleased with him. 
You must help poor Pedro distribute the chocolates into the small boxes in such a way that every small box is completely full, and contains only one brand of chocolates. A number of chocolates may however be left unassigned to any box (Pedro will keep these chocolates to himself).

Input

The input contains several instances of the problem. Each instance consists of three lines. The first line contains two integers M and L that indicate respectively the number of chocolates Mindt and Lilka Pedro bought (0 <= M, L <= 1000). The next line contains an integer N representing the number of small boxes Pedro has (N <= M+L). The third line contains N integers indicating the capacity Ci > 0 of box number i (that is, the number of chocolates needed to fill that box). The end of input is indicated by M = L = 0.

Output

For each instance of the input your program must produce one line of output. If it is possible to distribute the chocolates as defined in the problem statement, print the number of boxes to be filled with Mindt chocolate, followed by a space, followed by the list of box numbers, in ascending order. Each box number in the list should be followed by a space. If it is impossible to distribute the chocolates,print "Impossible to distribute". If more than one solution exists, print any one.

Sample Input

12 9 
4
5 2 8 5
100 120
5 21 32 110 54 3
0 0

Sample Output

3 1 2 4 
Impossible to distribute

题意:有M块Mindt巧克力,L块Lilka巧克力,N个有容量的盒子,求这些巧克力是否能装满盒子,并且每个盒子只装有一种巧克力。如果可以则从小到大输出装有Mindt巧克力的盒子序号。

 

题解:01背包。Mindt巧克力进行01背包之后,如果L块巧克力能把剩下的盒子装满那么可行,反之不行。此题还涉及到背包路径,仔细看代码。

 

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=10005;
int box[maxn],dp[maxn],pre[maxn],sum;
vector<int> v;
void dfs(int j);
int main()
{
   int i,j,M,L,n;
   while(~scanf("%d%d",&M,&L))
   {
    if(!M&&!L) break;
    {
     sum=0;
     v.clear();
     fill(pre,pre+maxn,0);
     fill(dp,dp+maxn,0);
    }
    scanf("%d",&n);
    for(i=1;i<=n;i++) scanf("%d",&box[i]),sum+=box[i];
    for(i=1;i<=n;i++)
     for(j=M;j>=box[i];j--)
      if(dp[j]<dp[j-box[i]]+box[i])
        {
         dp[j]=dp[j-box[i]]+box[i];
         pre[j]=i;
        }
    dfs(dp[M]);
    if(sum<=L)
     {
      printf("%d",v.size());
      for(i=v.size()-1;i>=0;i--) printf(" %d",v[i]);
      printf("\n");
     }
    else printf("Impossible to distribute\n");
   }

   return 0;
}
void dfs(int j)
{
 if(!j) return ;
 v.push_back(pre[j]);
 sum-=box[pre[j]];
 dfs(j-box[pre[j]]);
}

 

poj 1293 Duty Free Shop

标签:font   desc   sed   gif   each   容量   ring   signed   lin   

原文地址:https://www.cnblogs.com/VividBinGo/p/11354219.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!