# [POJ 1275] Cashier Employment【差分约束系统+二分答案】

• 2018-01-06
• 0
• 3

## Problem:

 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.

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.

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`

Source

Tehran 2000

## Solution:

• S[i] - S[i - 8] ≥ R[i] (8 ≤ i ≤ 24) …… i)
• S[i] - S[i + 16] ≥ R[i] - S (1 ≤ i ≤ 8) …… ii)

• 0 ≤ S[i] - S[i - 1] ≤ A[i] (1 ≤ i ≤ 24)，可化为
• S[i] - S[i - 1] ≥ 0 (1 ≤ i ≤ 24) …… iii)
• S[i - 1] - S[i] ≥ -A[i] (1 ≤ i ≤ 24) ……iv)

• S[i] - S[i + 16] ≥ R[i] - ans (1 ≤ i ≤ 8) …… v)

• S ≥ ans …… vi)

## Code: O(kElogN), E为总边数(=73) [136K, 0MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<algorithm>
using namespace std;

int T, N;
int R, A;
// R[] is the number of required cashiers at each hour
// A[] is the number of applicants at each hour

struct Edge{
int np, val;
Edge *nxt;
};

struct Graph{
Edge *V, E;
int static_tope, tope;

inline void clear() {tope = 0, memset(V, 0, sizeof(V));}

inline void addedge(int u, int v, int w){
E[++tope].np = v, E[tope].val = w;
E[tope].nxt = V[u], V[u] = &E[tope];
}
} G;

struct qnode{
int id, step;

qnode() {}
qnode(int id, int step): id(id), step(step) {}
};

struct queue{
#define inc(x) (x) = ((x) == 29 ? 1 : (x) + 1)
qnode node;
int fr, re;

inline void clear() {fr = re = 0;}

inline bool empty() {return fr == re;}

inline void push(const qnode &x) {node[re] = x, inc(re);}

inline void pop() {inc(fr);}

inline qnode front() {return node[fr];}
} q;

int dis;
bool inq;

inline bool SPFA(int key){
memset(dis, 0xc0, sizeof(dis));
memset(inq, 0, sizeof(inq)), q.clear();
q.push(qnode(0, 1)), inq = 1, dis = 0;
while(!q.empty()){
qnode u = q.front();
q.pop(), inq[u.id] = 0;
for(register Edge *ne = G.V[u.id]; ne; ne = ne->nxt)
if(dis[u.id] + ne->val > dis[ne->np]){
dis[ne->np] = dis[u.id] + ne->val;
if(u.step == 25) return 0;
if(!inq[ne->np]) q.push(qnode(ne->np, u.step + 1)), inq[ne->np] = 1;
// Beware of those debased mistakes such as forgetting to set in-queue flags
}
}
return 1;
}

inline void backupStatic() {G.static_tope = G.tope;}

inline void delExtra(){
if(G.tope == G.static_tope) return;  // Do not delete at the first time when the extra edges are not even added
G.tope = G.static_tope;
G.V = G.V->nxt;
for(register int i = 1; i <= 8; i++) G.V[i + 16] = G.V[i + 16]->nxt;
}

inline bool check(int key){
delExtra();
// key == S ==> S >= key && key >= S
for(register int i = 1; i <= 8; i++) G.addedge(i + 16, i, R[i] - key);
// S[i] + S - S[i + 16] >= R[i] ==> S[i] >= S[i + 16] + R[i] - S >= S[i + 16] + R[i] - key
return SPFA(key);
}

int main(){
scanf("%d", &T);
while(T--){
for(register int i = 1; i <= 24; i++) scanf("%d", R + i);
scanf("%d", &N);
memset(A, 0, sizeof(A));
for(register int i = 1; i <= N; i++){
int ti;
scanf("%d", &ti);
A[ti + 1]++;  // Beware of the subsription shift
}
G.clear();
for(register int i = 1; i <= 24; i++) G.addedge(i - 1, i, 0), G.addedge(i, i - 1, -A[i]);
// 0 <= S[i] - S[i - 1] <= A[i] ==> S[i] >= S[i - 1] && S[i - 1] >= S[i] - A[i]
for(register int i = 8; i <= 24; i++) G.addedge(i - 8, i, R[i]);
// S[i] - S[i - 8] >= R[i] ==> S[i] >= S[i - 8] + R[i]
backupStatic();  // Backup the static part of the edges
int lft = 0, rt = N + 1;
while(lft < rt){  // Run binary search to find the minimum number of total cashiers
int mid = lft + rt >> 1;
if(check(mid)) rt = mid;
else lft = mid + 1;
}
if(lft > N) puts("No Solution");
else printf("%d\n", lft);
}
return 0;
}
```

#### 评论

• ###### ZqlwMatt 2018-03-28 18:55回复

%%%

• ###### ZqlwMatt 2018-03-28 19:25回复

跑最长路的时候应该判正环 • ###### willem 2018-03-28 19:38回复

谢谢bjz大佬回复！！！
锅已修复。。 另：%%%%%bjz

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai