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. nyoj_5:Binary String Matching

    简单字符串匹配 题目链接 #include<iostream> #include<cstring> using namespace std; int fun(char*aa,c ...

  2. 利用Fiddler修改请求信息通过Web API执行操作(Action)实例

    本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复261或者20170724可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong.me ...

  3. spring mvc3 静态文件放在WEB-INF下无法访问解决和解决@Controller失效问题

    今天整合spring+mybatis+easyui碰到的问题 将easyui放在WEB-INF下后无法访问, 解决:需配置<mvc:resources mapping="/easyui ...

  4. Java中的clone()----深复制,浅复制

    这篇文章主要介绍了Java中对象的深复制(深克隆)和浅复制(浅克隆) ,需要的朋友可以参考下 1.浅复制与深复制概念 ⑴浅复制(浅克隆) 被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他 ...

  5. 学生成绩管理系统——C语言实现

    一.功能实现: 0.浏览学生信息 1.输入学生信息 2.增加学生信息 3.修改学生信息 4.删除学生信息 5.按学号查询 6.按班级查询 7.按姓名查询 8.按课堂名称查询 9.按总分高低排序 10. ...

  6. CentOS安装配置MySql数据库

    CentOS版本7.2,MySql版本5.7 1.下载MySql安装源     wget https://dev.mysql.com/get/mysql57-community-release-el7 ...

  7. 【 js 基础 】【 源码学习 】backbone 源码阅读(二)

    最近看完了 backbone.js 的源码,这里对于源码的细节就不再赘述了,大家可以 star 我的源码阅读项目(source-code-study)进行参考交流,有详细的源码注释,以及知识总结,同时 ...

  8. Hadoop新生报到(一) hadoop2.6.0伪分布式配置详解

    首先先不看理论,搭建起环境之后再看: 搭建伪分布式是为了模拟环境,调试方便. 电脑是win10,用的虚拟机VMware Workstation 12 Pro,跑的Linux系统是centos6.5 , ...

  9. 关于个人编辑器sublime text3使用指南

    用过了好多编辑器,前些年用的zend studio,phpstorm近两年转为nodepad++(因为写的语言种类比较多了,shell,python,php,前端等),相对于nodepad++,  s ...

  10. MySQL checkpoint深入分析

    1.日常关注点的问题 2.日志点分析 3.checkpoint:脏页刷盘的检查点 4.模糊检查点发生条件 1.master thread checkpoint 2.flush_lru_list che ...