POJ 2901 Hotel

 

http://acm.pku.edu.cn/JudgeOnline/problem?id=2901
Hotel
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 561 Accepted: 156

Description

Zebel, the tour coordinator, has reserved a limited number of hotel rooms for his clients. Rooms have different capacities and naturally, different prices. Zebel decides to find the least cost assignment of the tour participants to the available rooms. His strategy is to fill the rooms with appropriate collection of people to minimize the overall room cost, but he is facing some restrictions that no two people of different sex that are not married may stay in the same room, and if a room is assigned to a married couple, no other person may stay in that room. Note that it is not necessary to put a married couple in the same room. It is also possible that we do not fill a room to its capacity. 

You are to write a program to help Zebel find a least cost assignment of the tour participants to the reserved hotel rooms. 

Input

The only number in the first line is t, the number of test cases that follow. The first line of each test case contains four integer numbers, 0 ≤ m ≤ 500 the number of male tour participants, 0 ≤ f ≤ 500 the number of female tour participants, 0 ≤ r ≤ 500 the number of rooms reserved by Zebel, and c ≥ 0 which is the number of marriage relations between tour participants. Note that polygamy is not allowed in the tour; i.e. each participant is either single or has a unique mate. 

The description of the reserved rooms comes on the following r lines. Each line describes a room, by two integer numbers 1 ≤ bi ≤ 5, and 1 ≤ pi ≤ 1000, which are the capacity and price of this room. 

Output

For each test case in the input, output the minimum cost of assigning the rooms to the tour participants. If this is not possible, output the phrase "Impossible" instead. 

Sample Input

2
2 1 3 1
3 5
2 10
2 4
1 1 1 0
1 4

Sample Output

9
Impossible

Source

/*
(1)这题一开始想到的是贪心,方法是:根据当前剩余的male,female,couple数来求每个房价的最大单人价格,即用
房间的价格除以这个房间可以放入的实际人数,然后取最大值的房间放入人数,一直取到所有房间都被用上或者
剩余的male,female,couple数是0,但是这样做后来经过验证是不正确的,这个问题不具备贪心性质,因为当前最
优不能保证全局最优,所以不能用贪心进行求解
(2)后来想到的是贪心不行的话,动态规划肯定行。但是动态规划的问题是可能超时,因为有四维(room, male,female,couple)
这样就是n ^ 4,肯定会超时。后来看到讨论里面有人给了个提示,可以将couple那一维的上限减少到最多是1,因为设想
假如一共分了k(k >= 2)个房间给所有couple,那么我们总可以通过将所有couple中的男子放在一起住以及将所有女子放在
一起住来减少房间的使用量,这样总的消费数肯定不超过原来的消费数。至于为什么有可能需要开出一个房间给一对couple,
是为了避免如下情况:假设有三对couple,分别放在容量为2,2,2的三个房间中,那么肯定只能合并处理两队couple了。基于
(3)通过以上分析可以将couple维的上限减少到1.那么这个就是一个典型的背包问题了
dp(rp, mp, fp, cp)表示利用1~rp个房间,放入mp,fp,cp可以取得的最小费用,则
if(rp >= 1) 则dp(rp, mp, fp, cp) = minv(dp(rp, mp, fp, cp), dp(rp - 1, mp - minv(cap(rp), fp, mp) + price(rp))
if(mp >= 1) 则dp(rp, mp, fp, cp) = minv(dp(rp, mp, fp, cp), dp(rp - 1, mp, fp - minv(cap(rp), fp) + price(rp))
if(cp >= 1 && cap(rp) >= 2)  则dp(rp, mp, fp, cp) = minv(dp(rp, mp, fp, cp), dp(rp - 1, mp - 1, fp - 1, 0) + price(rp))
上面三种情况分别为在当前屋子放入男子,放入女子,放入couple
由于每个dp(rp)状态只与dp(rp - 1)有关,因此可以利用倒序遍历减少存储空间,即存储空间开辟为dp[m][f][2],遍历的时候
第一层循环从1致r, 第二层循环从m致0,第三层循环从f致0,第四层循环从1致0即可,在每个第一层结束的时候取dp[m][f][0],dp[m - 1][f - 1][1]
中的最小者即为最后所求
(4)初始化:dp[0][0][0]初始化为1,其他所有值初始化为最大值即可
*/
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值