POJ 1293 Duty Free Shop(背包记录路径)
2015-05-28 22:21
197 查看
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
Sample Output
这题的做法就是对于M类型的巧克力做01背包,记录路径,
至于判断可行性可以背包后的容量来搞。简单题。
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类型的巧克力做01背包,记录路径,
至于判断可行性可以背包后的容量来搞。简单题。
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<bitset> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; const int INF = 0x3f3f3f3f; int M,L,n,cnt,w; int dp[1100]; int c[1100],pre[1100]; int ans[1100]; void dfs(int val) { if(val==0) return ; dfs(val-c[pre[val]]); ans[cnt++]=pre[val]; } int main() { while(~scanf("%d%d",&M,&L)&&(M+L)) { scanf("%d",&n); int sum=0; cnt=0; REPF(i,1,n) { scanf("%d",&c[i]); sum+=c[i]; } if(sum>M+L) { puts("Impossible to distribute"); continue; } CLEAR(pre,-1); CLEAR(dp,-1); dp[0]=0; for(int i=1;i<=n;i++) { for(int j=M;j>=c[i];j--) { if(dp[j-c[i]]!=-1&&dp[j-c[i]]+c[i]>dp[j]) { dp[j]=dp[j-c[i]]+c[i]; pre[j]=i; } } } for(int i=M;i>=0;i--) if(dp[i]!=-1) { w=i; break; } if(dp[w]+L<sum) { puts("Impossible to distribute"); continue; } dfs(w); printf("%d",cnt); REP(i,cnt) printf(" %d",ans[i]); puts(""); } return 0; } /* */
相关文章推荐
- 深入剖析Tomcat会话机制
- Linux 进程--父进程查询子进程的退出状态
- 容器+AOP 实现动态部署(一)
- 郁闷的 linux error: 13: Permission denied 引发Linux中的文件权限详解
- Photoshop - 描边
- Visual Studio 2013中在IE浏览器浏览localhost网站时候,发现会不断有下面链接的请求
- Linux 信号机制 (二)
- Hadoop的调度器总结
- 个人日常linux相关工作问题汇总
- 使用wecenter&SAE搭建问答社区型网站
- Hadoop1.2.1 配置文件详解
- keepalived+nginx高可用配置
- apache 访问控制
- Cygwin:Windows下的Linux终端模拟器
- Cygwin:Windows下的Linux终端模拟器
- Linux中基于ptrace的外挂程序设计
- Linux-(20)Linux的文件系统<2>
- CentOS 忘记root密码,解决方法
- linux 命令
- Linux 驱动之 EXPORT_SYMBOL 函数以及 2.6 内核 Unknown symbol bug 解决办法