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. pug模板引擎(原jade)

    前面的话 为什么要引入pug,pug有什么特别之处呢?有一些嵌套层次较深的页面,可能会出现巢状嵌套,如下图所示 在后期维护和修改时,一不小心少了一个尖括号,或者某个标签的开始和闭合没有对应上,就会导致 ...

  2. 登录界面Demo

    今天记载一个Demo,这个是我练习项目中用到,供新手看看,界面图:

  3. (转)Sublime Text2 快捷键汇总

    场景:最近在编写项目中越发的感觉到一个得心应手的编辑器是多么的重要,而sublime,无疑是让我用着最舒服,最有感觉的编辑器了! 1 快捷键总结 一个好的编辑器,能大大提高编程的效率.如果能熟知软件的 ...

  4. swift3.0 CoreGraphics绘图-实现画板

    swift3.0对绘图的API进行了优化,看起来更swift了. 看下UI的构造.设置画笔粗细.清空面板和保存到本地 下面直接看画板文件 这里我做的比较复杂,记录触摸到的每个点,再连成路径,其实直接用 ...

  5. [BZOJ1500][NOI2005]维修数列---解题报告

    Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...

  6. angularJS的$http.post请求,.net后台接收不到参数值的解决方案

    JS通用部分 var shoppingCartModule =angular.module('starter', ['ionic'], function ($httpProvider) { // Us ...

  7. OpenFlow协议1.0及1.3版本分析

    OpenFlow是SDN控制器和交换之间交流的协议,在SDN领域有着十分重要的地位. OpenFlow协议发展到现在已经经过了1.0.1.3.1.4等版本.其中1.0和1.3版本使用的是最为广泛的. ...

  8. css鼠标样式cursor

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Hadoop(一)Centos7虚拟机网络配置

    Centos7虚拟机网络配置(桥接模式) 一 VirtualBox提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式). 1 桥接模式(br ...

  10. Qt For Android 开发环境配置

    想了想,还是再写一篇关于Qt for Android开发环境配置的教程. 准备:Java jdk,Android sdk,Android adb,Android ndk,Android ant,Qt ...