poj 2901 Hotel

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

Tehran 2005

题意&思路:无聊的题目,纯最短路。dijsktra+heap优化。存下来当模板

 1 /*
 2  * Author:  Joshua
 3  * Created Time:  2014年10月06日 星期一 14时25分35秒
 4  * File Name: poj2901.cpp
 5  */
 6 #include<cstdio>
 7 #include<algorithm>
 8 #include<cstring>
 9 #include<iostream>
10 using namespace std;
11 #define maxn 515
12 #define inf 0x3f3f3f3f
13 typedef long long LL;
14 int T,n,m,r,c,ans;
15 int b[maxn],p[maxn],f[maxn][maxn];
16
17 void init()
18 {
19     scanf("%d%d%d%d",&n,&m,&r,&c);
20     for (int i=1;i<=r;++i)
21         scanf("%d%d",&b[i],&p[i]);
22 }
23
24 void updata(int&x ,int y)
25 {
26     if (y<x) x=y;
27 }
28
29 void solve()
30 {
31     int tb,tp,tans,tk;
32     ans=inf;
33     for (int i=0;i<=n+10;++i)
34         memset(f[i],0x3f,(m+10)<<2);
35     f[0][0]=0;
36     for (int i=1;i<=r;++i)
37     {
38         tb=b[i];tp=p[i];
39         for (int t1=n+5;t1>=0;t1--)
40             for (int t2=m+5;t2>=0;t2--)
41             {
42                 if (tb<=t1) updata(f[t1][t2],f[t1-tb][t2]+tp);
43                 if (tb<=t2) updata(f[t1][t2],f[t1][t2-tb]+tp);
44             }
45     }
46     for (int i=n;i<=n+5;++i)
47         for (int j=m;j<=m+5;++j)
48             ans=min(ans,f[i][j]);
49     if (c%2==0) return;
50     tk=0;
51     b[0]=6;p[0]=inf;
52     for (int i=1;i<=r;++i)
53         if (b[i]>=2  && (p[i]<=p[tk] || (p[i]==p[tk] && b[i]<b[tk])))
54             tk=i;
55     if (!tk) return;
56     n--;m--;
57     for (int i=0;i<=n+10;++i)
58         memset(f[i],0x3f,(m+10)<<2);
59     f[0][0]=0;
60     for (int i=1;i<=r;++i)
61     {
62         if (i==tk) continue;
63         tb=b[i];tp=p[i];
64         for (int t1=n+5;t1>=0;t1--)
65             for (int t2=m+5;t2>=0;t2--)
66             {
67                 if (tb<=t1) updata(f[t1][t2],f[t1-tb][t2]+tp);
68                 if (tb<=t2) updata(f[t1][t2],f[t1][t2-tb]+tp);
69             }
70     }
71     for (int i=n;i<=n+5;++i)
72         for (int j=m;j<=m+5;++j)
73             ans=min(ans,f[i][j]+p[tk]);
74 }
75
76 int main()
77 {
78     scanf("%d",&T);
79     for (int i=1;i<=T;++i)
80     {
81         init();
82         solve();
83         if (ans!=inf) printf("%d\n",ans);
84         else printf("Impossible\n");
85     }
86     return 0;
87 }
时间: 2024-10-21 11:41:25

poj 2901 Hotel的相关文章

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

题目传送门 1 /* 2 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 3 输入 2 a b:将[a,a+b-1]的房间清空 4 线段树(区间合并):lsum[]统计从左端点起最长连续空房间数,rsum[]类似,sum[]统计区间最长连续的空房间数, 5 它有三种情况:1.纯粹是左端点起的房间数:2.纯粹是右端点的房间数:3.当从左(右)房间起都连续时,加上另一个子节点 6 从左(右)房间起的数,sum[]再求最大值更新维护.理解没错,表达能力不足 7 详细解释:htt

POJ 3667 Hotel(线段树)

POJ 3667 Hotel 题目链接 题意:有n个房间,现在有两个操作 1.找到连续长度a的空房间,入住,要尽量靠左边,如果有输出最左边的房间标号,如果没有输出0 2.清空[a, a + b - 1]的房间 思路:线段树的区间合并,记录下左边连续最长和右边连续最长空房间,和每一段的最大值,这样pushup的时候就是进行区间合并,注意查询的时候由于是要尽量左,所以先查左孩子,在查横跨左右的,在查右孩子 代码: #include <cstdio> #include <cstring>

POJ 3667 Hotel

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16782   Accepted: 7303 Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie

poj 3667 Hotel - 线段树

The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their v

POJ 3667 Hotel 【线段树 区间合并 + Lazy-tag】

Hotel Time Limit: 3000MS Memory Limit: 65536K 链接:POJ 3667   Description The cows are journeying north to ThunderBay in Canada to gain cultural enrichment and enjoy a vacation on the sunnyshores of Lake Superior. Bessie, ever the competent travel agen

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

题目传送:Hotel 思路:线段树,区间合并,区间替换,查询最左断点,看胡浩版本的线段树好几天了,今天看这个看了好久,慢慢来吧,具体都写在注释里了 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #inc

(简单) POJ 3667 Hotel,线段树+区间合并。

Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Stree

POJ 3667 Hotel(区间合并)

题目链接:http://poj.org/problem?id=3667 题意:宾馆有n个房间.有人来入住.共有2种操作 输入1和d,表示查询最左的连续d个空房间数的起始位置. 输入2,x和d,表示将从x开始长度为d的连续的房间清空. 思路:裸的区间合并.每个结点区间[l,r]存从左端点l开始向右最大连续空房间数lm,从右端点r开始向左最大连续空房间数rm和当前区间内最大连续空房间数. 代码: #include <iostream> #include <stdio.h> #inclu

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

Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Stree