Cashier Employment
Time Limit: 1000MS | Memory Limit: 10000K | |
Description
A supermarket in Tehran is open 24 hours a day every day and needs a number of cashiers to fit its need. The supermarket manager has hired you to help him, solve his problem. The problem is that the supermarket needs different number of cashiers at different times of each day (for example, a few cashiers after midnight, and many in the afternoon) to provide good service to its customers, and he wants to hire the least number of cashiers for this job.
The manager has provided you with the least number of cashiers needed for every one-hour slot of the day. This data is given as R(0), R(1), ..., R(23): R(0) represents the least number of cashiers needed from midnight to 1:00 A.M., R(1) shows this number for duration of 1:00 A.M. to 2:00 A.M., and so on. Note that these numbers are the same every day. There are N qualified applicants for this job. Each applicant i works non-stop once each 24 hours in a shift of exactly 8 hours starting from a specified hour, say ti (0 <= ti <= 23), exactly from the start of the hour mentioned. That is, if the ith applicant is hired, he/she will work starting from ti o'clock sharp for 8 hours. Cashiers do not replace one another and work exactly as scheduled, and there are enough cash registers and counters for those who are hired.
You are to write a program to read the R(i) 's for i=0..23 and ti 's for i=1..N that are all, non-negative integer numbers and compute the least number of cashiers needed to be employed to meet the mentioned constraints. Note that there can be more cashiers than the least number needed for a specific slot.
The manager has provided you with the least number of cashiers needed for every one-hour slot of the day. This data is given as R(0), R(1), ..., R(23): R(0) represents the least number of cashiers needed from midnight to 1:00 A.M., R(1) shows this number for duration of 1:00 A.M. to 2:00 A.M., and so on. Note that these numbers are the same every day. There are N qualified applicants for this job. Each applicant i works non-stop once each 24 hours in a shift of exactly 8 hours starting from a specified hour, say ti (0 <= ti <= 23), exactly from the start of the hour mentioned. That is, if the ith applicant is hired, he/she will work starting from ti o'clock sharp for 8 hours. Cashiers do not replace one another and work exactly as scheduled, and there are enough cash registers and counters for those who are hired.
You are to write a program to read the R(i) 's for i=0..23 and ti 's for i=1..N that are all, non-negative integer numbers and compute the least number of cashiers needed to be employed to meet the mentioned constraints. Note that there can be more cashiers than the least number needed for a specific slot.
Input
The first line of input is the number of test cases for this problem (at most 20). Each test case starts with 24 integer numbers representing the R(0), R(1), ..., R(23) in one line (R(i) can be at most 1000). Then there is N, number of applicants in another line (0 <= N <= 1000), after which come N lines each containing one ti (0 <= ti <= 23). There are no blank lines between test cases.
Output
For each test case, the output should be written in one line, which is the least number of cashiers needed.
If there is no solution for the test case, you should write No Solution for that case.
If there is no solution for the test case, you should write No Solution for that case.
Sample Input
1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 5 0 23 22 1 10
Sample Output
1
————————————————————嗨森的分割线————————————————————
前言:这是黑书上最短路问题的一道题目。其实并不算难,只是看是否熟悉掌握了差分约束的技巧。
思路:首先将一天编号为1~24号区间。代表那么对于0~24的点 i ,S[i]代表前 i 个小时的雇佣总数。S[i]- S[i-1]就代表在 i 时刻雇佣的出纳员数。
首先人数为正:
S[i] - S[i-1] >= 0
然后根据每个时刻申请者的个数:
S[i] - S[i-1] <= t[i] 这是因为在时刻 i 最多只能雇佣所有申请者。
接下来就是对时刻表的约束条件。
举例来说,对于时刻8:
时刻0~时刻8之间,时刻0雇佣的已经工作8小时下班,因此:
S[i] - S[i-8] >= r[i]
但是对于时刻0~7,他们是从昨天晚上工作到现在留下的。
将数轴延长一天:
j i
| 1 | 2 | 3 | ... | 24 | 1 | 2 | 3 | ......| 24 |
0----1----2----3---- ... ----24----1----2----3---- ... ----24
S[i]+sum - S[j] >= r[i]
加上昨天的就行了!
sum本来是未知量,现在作为已知量引入,那么必须满足:
S[24] - S[0] >= sum
对于sum二分答案即可。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
const int D = 24, N = 25, M = N*N;
struct Node {
int v, w, next;
}edge[M];
int q[N], dis[N], head[N], enq[N], t[N], r[N], tot;
bool inq[N], flag;
void add(int u, int v, int w)
{
edge[tot].v = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
}
bool spfa(int sum)
{
for(int i = 0 ; i < N; i++) {
dis[i] = -INF;
inq[i] = enq[i] = 0;
}
dis[0] = 0;
int fron = 0, rear = 1;
q[fron] = 0;
inq[0] = 1;
enq[0] ++;
while(fron < rear) {
int u = q[fron%N]; fron ++;
inq[u] = 0;
for(int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
if(dis[v] < dis[u] + edge[i].w) {
dis[v] = dis[u] + edge[i].w;
if(!inq[v]) {
q[rear%N] = v; rear++;
inq[v] = true;
enq[v]++;
if(enq[v] > 24) return false;
}
}
}
}
if(dis[D] - dis[0] == sum) return true;
return false;
}
void build(int sum)
{
memset(head, -1, sizeof(head));
tot = 0;
//S[i] - S[i-1] >= 0
//S[i] - S[i-1] <= t[i]
for(int i = 1; i <= D; i++) {
add(i-1, i, 0);
add(i, i-1, -t[i]);
}
add(0, D, sum);//S[24] - S[0] >= sum
for(int i = 8; i <= D; i++) {
add(i-8, i, r[i]);
}//S[i] - S[j] >= r[i]
for(int i = 0; i < 8; i++) {
add(i+16, i, r[i] - sum);
}//s[i]+sum - s[i+16] >= r[i]
}
bool check(int sum)
{
build(sum);
if(spfa(sum)) return true;
else return false;
}
int main()
{
#ifdef J_Sure
// freopen("000.in", "r", stdin);
// freopen(".out", "w", stdout);
#endif
int T;
scanf("%d", &T);
while(T--) {
flag = false;
for(int i = 1; i <= D; i++) {
scanf("%d", &r[i]);
}
int n;
scanf("%d", &n);
int x;
memset(t, 0, sizeof(t));
for(int i = 0; i < n; i++) {
scanf("%d", &x);
t[x+1] ++;
}
int left = 0, right = n+1;
while(left < right) {
int m = (left+right)>>1;
if(check(m)) right = m;
else left = m+1;
}
if(right == n+1) puts("No Solution");
else printf("%d\n", right);
}
return 0;
}