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. hdu_5963:朋友

    刚看到这题时感觉是树上博弈,然后我开始用一维的数据找规律.发现在一维的树上,如果把各边的值合在一起当成一个二进制数,那么,ans只与奇偶性有关,于是,我提出了一个比较大胆的假设:若连接在root上的所 ...

  2. SLAM中的优化理论(一)—— 线性最小二乘

    最近想写一篇系列博客比较系统的解释一下 SLAM 中运用到的优化理论相关内容,包括线性最小二乘.非线性最小二乘.最小二乘工具的使用.最大似然与最小二 乘的关系以及矩阵的稀疏性等内容.一方面是督促自己对 ...

  3. RxSwift 系列(五) -- Filtering and Conditional Operators

    前言 本篇文章将要学习RxSwift中过滤和条件操作符,在RxSwift中包括了: filter distinctUntilChanged elementAt single take takeLast ...

  4. jenkins-APP打包页面展示二维码

    背景: 客户要求在APP打包页面展示二维码.虽然感觉这个功能很鸡肋,但是还是加上吧. 效果展示: 配置: 在上图中,106对应的内容是BuildName,我们可以通过build-name-setter ...

  5. iOS动画学习-视觉效果

    CALayer不仅仅是iOS动画学习-CALayer中介绍的那些内容,他还有一些其他属性,比如shadowColor,borderWidth,borderColor等等,这些属性我们只需要简单点设置就 ...

  6. H5投放在朋友圈广告做压力测试

    一.环境 MacOS Sierra 二.背景 朋友圈广告投放的H5需要做ab压测,这里不赘述. 具体官方文档如下:http://ad.weixin.qq.com/learn/n10 三.正文 (1)别 ...

  7. js存款计算器原生小demo

    大家好,本人是初入前端的一枚程序猿,深知js底层开发的重要性,这也是我的软肋所在(曾经以为),渐渐的明白了一个道理,饭要一口口吃,路要一步步走,这也是我想告诉给所有刚刚进入IT行业的技术员们,沉下心, ...

  8. [HNOI2013]游走 期望+高斯消元

    纪念首道期望题(虽说绿豆蛙的归宿才是,但是我打的深搜总觉得不正规). 我们求出每条边的期望经过次数,然后排序,经过多的序号小,经过少的序号大,这样就可以保证最后的值最小. 对于每一条边的期望经过次数, ...

  9. Sybase数据库的连接,JNDI配置,Hibernate配置

    最近的一个项目就是移植老项目的代码,有一个模块用的是Sybase数据库,我表示从来没接触过,更不用说怎么用了.再者这东西都是几乎被淘汰的东西了,而且网上搜到的东西简直了,全是复制粘贴的. 一.使用工具 ...

  10. EF对于已有数据库的Code First支持

    EF对于已有数据库的Code First支持 原文链接 本文将逐步介绍怎样用Code First的方式基于已有数据库进行开发.Code First支持你使用C#或者VB.Net定义类.并使用数据模型标 ...