Hotel
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 859   Accepted: 280

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

 
题意:有n个男人,m个女人一起住宿,其中有r个房间,这些人中有c对夫妇。每对夫妇在一间房间的时候房间不能有其他人,不是一对的男女不能同房。没有一夫多妻、一妻多夫。每个房间有容量bi,有费用pi。请问让全部人住进去最少需要多少钱。
思路:如果有两对夫妇的话,分别需要2+2的房间,如果把他们分成2男2女,也是分成2+2的房间,且房间还可能住其他人,所以显然更优,所以最优的情况下最多只有c%2的夫妇住在一起,那么只要用动态规划计算没有夫妻住在一起的情况和只有一对夫妻住在一起的情况就行了。用 f[i][j]表示i个男人,j个女人住在旅馆里最少需要多少钱。思路很简单,直接看代码就能懂了。
 /*
* Author: Joshua
* Created Time: 2014年10月06日 星期一 14时25分35秒
* File Name: poj2901.cpp
*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
#define maxn 515
#define inf 0x3f3f3f3f
typedef long long LL;
int T,n,m,r,c,ans;
int b[maxn],p[maxn],f[maxn][maxn]; void init()
{
scanf("%d%d%d%d",&n,&m,&r,&c);
for (int i=;i<=r;++i)
scanf("%d%d",&b[i],&p[i]);
} void updata(int&x ,int y)
{
if (y<x) x=y;
} void solve()
{
int tb,tp,tans,tk;
ans=inf;
for (int i=;i<=n+;++i)
memset(f[i],0x3f,(m+)<<);
f[][]=;
for (int i=;i<=r;++i)
{
tb=b[i];tp=p[i];
for (int t1=n+;t1>=;t1--)
for (int t2=m+;t2>=;t2--)
{
if (tb<=t1) updata(f[t1][t2],f[t1-tb][t2]+tp);
if (tb<=t2) updata(f[t1][t2],f[t1][t2-tb]+tp);
}
}
for (int i=n;i<=n+;++i)
for (int j=m;j<=m+;++j)
ans=min(ans,f[i][j]);
if (c%==) return;
tk=;
b[]=;p[]=inf;
for (int i=;i<=r;++i)
if (b[i]>= && (p[i]<=p[tk] || (p[i]==p[tk] && b[i]<b[tk])))
tk=i;
if (!tk) return;
n--;m--;
for (int i=;i<=n+;++i)
memset(f[i],0x3f,(m+)<<);
f[][]=;
for (int i=;i<=r;++i)
{
if (i==tk) continue;
tb=b[i];tp=p[i];
for (int t1=n+;t1>=;t1--)
for (int t2=m+;t2>=;t2--)
{
if (tb<=t1) updata(f[t1][t2],f[t1-tb][t2]+tp);
if (tb<=t2) updata(f[t1][t2],f[t1][t2-tb]+tp);
}
}
for (int i=n;i<=n+;++i)
for (int j=m;j<=m+;++j)
ans=min(ans,f[i][j]+p[tk]);
} int main()
{
scanf("%d",&T);
for (int i=;i<=T;++i)
{
init();
solve();
if (ans!=inf) printf("%d\n",ans);
else printf("Impossible\n");
}
return ;
}

poj2901 Hotel的更多相关文章

  1. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  2. ACM: Hotel 解题报告 - 线段树-区间合并

    Hotel Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description The ...

  3. HDU - Hotel

    Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and e ...

  4. 【POJ3667】Hotel

    Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and e ...

  5. POJ-2726-Holiday Hotel

    Holiday Hotel   Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8302   Accepted: 3249 D ...

  6. Method threw 'org.hibernate.exception.SQLGrammarException' exception. Cannot evaluate com.hotel.Object_$$_jvst485_15.toString()

    数据库字段和类Object属性不匹配,Method threw 'org.hibernate.exception.SQLGrammarException' exception. Cannot eval ...

  7. poj 3667 Hotel(线段树,区间合并)

    Hotel Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 10858Accepted: 4691 Description The ...

  8. [POJ3667]Hotel(线段树,区间合并)

    题目链接:http://poj.org/problem?id=3667 题意:有一个hotel有n间房子,现在有2种操作: 1 a,check in,表示入住.需要a间连续的房子.返回尽量靠左的房间编 ...

  9. 【BZOJ】【3522】【POI2014】Hotel

    暴力/树形DP 要求在树上找出等距三点,求方案数,那么用类似Free Tour2那样的合并方法,可以写出: f[i][j]表示以 i 为根的子树中,距离 i 为 j 的点有多少个: g[i][j]表示 ...

随机推荐

  1. 【NOIP模拟】table(动态规划)

    题目背景 SOURCE:NOIP2016-RZZ-2 T2 题目描述 给定一个 n×m 的矩阵,行列均从 1 开始标号. 一个矩阵被认为是稳定的,当且仅当对于任意的 2≤i≤n,第 i 行的数的和不小 ...

  2. python中全局变量和局部变量的一个小坑

    python 中全局变量和局部变量在使用过程中的一个容易出错的地方 什么是全局变量 python中,在函数外部声明的变量可以叫做全局变量. x = 10 def fn1(): pass fn1() 什 ...

  3. ORACLE - 系统参数调整

    一.内存调整 oracle 11g中,ORACLE把SGA与PGA统一管理,总和为memory_target参数的设定,也就是MAX(SGA+PGA)<= memory_target(当然可以在 ...

  4. 【.net 深呼吸】在配置节中使用元素集合

    前一篇博文中,老周介绍了自定义配置节的方法,本文咱们再往深一层,再看看如何在自定义的配置节中使用配置元素集合. 前面咱们说过,Configuration Section是特殊的配置元素,它可以包装一类 ...

  5. Tornado session 插件 pycket 定制时间和时间续租

    功能描述:10分钟用户没有任何操作,跳转到登录页面. 分析:这个功能用session就能实现(由于pycket 的session内容是存储在memcached或者redis里面的.所以,session ...

  6. 自定义Git之搭建Git服务器

    在远程仓库一节中,我们讲了远程仓库实际上和本地仓库没啥不同,纯粹为了7x24小时开机并交换大家的修改. GitHub就是一个免费托管开源代码的远程仓库.但是对于某些视源代码如生命的商业公司来说,既不想 ...

  7. Trailing Zeroes (III)

    You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...

  8. kafka在windows下的安装和配置

    博主最近在学习有关kafka的配置安装以及在spring的集成使用.但网上关于kafka的配置参考资料基本都是于linux下的配置,于是博主在整理了相关windows下kafka的配置记录在博客里.由 ...

  9. Luogu P1001 A+B Problem

    题目描述 输入两个整数a,b,输出它们的和(|a|,|b|<=10^9). 注意 1.pascal使用integer会爆掉哦! 2.有负数哦! 3.c/c++的main函数必须是int类型,而且 ...

  10. 分辨率验证工具 - 【Firesizer】的使用升级-Firefox-29.0

    今天打算使用Firesizer,但是在Firefox浏览器的右下角找不到Firesizer了. 在官网搜了搜(https://addons.mozilla.org/en-US/firefox/addo ...