关键词:差分约束 相关题目:[POJ1275]Cashier Employment

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&Output

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

Solution

为方便解题叙述,我们有以下约定:
    num[i] 为来应聘的在第i个小时开始工作的人数
    r[i] 为第i个小时至少需要的人数
    x[i] 为招到的在第i个小时开始工作的人数
    s[i] = x[1] + … + x[i]

根据题意有:
    0≤x[i]≤num[i]
    x[i] + x[i-1] + … + x[i-7] ≥ r[i] (题目中的连续工作8小时)

应用数组 s[]:
    s[i] – s[i-1] ≥ 0
    s[i-1] – s[i] ≥ –num[i]
    s[i] – s[i-8] ≥ r[i] , 9 ≤ i ≤ 24
    s[i] – s[i+16] ≥ r[i] – s[24],  1 ≤ i ≤ 8

最终答案: ans ≤ s[24]
通过二分枚举s[24],来检测是否满足条件,题目是求最小值,即求最长路,以0为源点。

如果中间出现正环,说明该答案的存在条件为: \exists 显然是矛盾的。 另外 对条件 S[ i ] - S[ i + 16\ ] ≥ R[ i ] - S[ 24 ] 建边即为:(因此1.是必须的)

1
2
add(0,24,x);
rep(i,1,8) add(i+16,i,r[i]-x);

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<cstdio>
#include<queue>
#include<cstring>
#define re register int
#define rep(i,a,b) for(re i=(a);i<=(b);++i)
const int N=25,M=1020;
int n,r[N],num[N],s[N];

std::queue<int> q;
int f[N],ru[N];
bool vis[N];
int cnt,nxt[M],to[M],w[M],head[N];
inline void add(int u,int v,int W){
nxt[++cnt]=head[u],head[u]=cnt,to[cnt]=v,w[cnt]=W;
}
#define travel(i,u) for(re i=head[u];i;i=nxt[i])

inline void build_Graph(int x){
memset(head,0,sizeof(head));
cnt=0;
rep(i,1,24){
add(i-1,i,0);
add(i,i-1,-num[i]);
}
add(0,24,x);
rep(i,1,8) add(i+16,i,r[i]-x);
rep(i,9,24) add(i-8,i,r[i]);
}//建图
inline bool spfa(int x){
re u,v;
while(!q.empty()) q.pop();
memset(f,~0x3f,sizeof(f));
memset(vis,0,sizeof(vis));
memset(ru,0,sizeof(ru));
q.push(0);
f[0]=0;
while(!q.empty()){
u=q.front(),q.pop(),vis[u]=0;
travel(i,u){
v=to[i];
if(f[v]<f[u]+w[i]){
f[v]=f[u]+w[i];
++ru[v];
if(!vis[v]) q.push(v),vis[v]=1;
if(ru[v]>24) return 0;//判正环
}
}
}
if(f[24]==x) return 1;
return 0;
}
inline void solve(){
int l=1,r=n,mid;
bool flag=0;
while(l<=r){
mid=(l+r)>>1;
build_Graph(mid);
if(spfa(mid)) flag=1,r=mid-1;
else l=mid+1;
}
if(flag) printf("%d\n",l);
else puts("No Solution");
}

int main(){
#ifndef ONLINE_JUDGE
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
int T;
scanf("%d",&T);
while(T--){
rep(i,1,24) scanf("%d",r+i),num[i]=0;
scanf("%d",&n);
int x;
rep(i,1,n) scanf("%d",&x),++num[x+1];
solve();
}
}