Cashier Employment
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1394 Accepted Submission(s): 631
Problem 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这是一道非常经典的差分约束系统的题目。建图有一定难度。 设d[i]表示从0到第i小时一共雇佣了多少人。 r[i]表示第i小时需要的工作人员,num[i]表示有num[i]个人可以在 第i个小时开始工作。 则有0<=d[i+1]-d[i]<=num[i+1]; 当i>=8时:d[i]-d[i-8]>=r[i];//因为i-8小时前雇佣的人都下班了,所以 第i-8至i小时间至少要雇佣r[i]人 当i<8时:d[23]-d[i+16]+d[i]>=r[i];//只要是第i+16小时后开始工作的 次日第i小时一定还在工作,d[23]-d[i+16]为上一天留下的工作人员, 在加上d[i],便是第i个小时在工作的人数,因此d[23]-d[i+16]+d[i]>=r[i] 因为时间时循环的,为了避免此处形成环,在这里加一个超级源点MAX, 使0与MAX相连而不是和23相连。超级原点处的d[MAX]设为0, 则有d[i]-d[MAX]>=0,d[0]-d[MAX]<=num[0]; 整理以上不等式得: i=0 to 22: d[i+1]-d[i]>=0; d[i]-d[i+1]>=-num[i+1]; i=8 to 23: d[i]-d[i-8]>=r[i]; i=0 to 7: d[i]-d[i+16]>=r[i]-d[23]; i=0 to 23: d[i]-d[MAX]>=0; i=0; d[MAX]-d[0]>=-num[0]; 这样出d[i]-d[i+16]>=r[i]-d[23]外就都整理成了符合条件的情况, 因为d[i]表示的是从一开始到i一共雇佣的人,所以求出d[23]就是该题的解 因为此题n的范围不会超过24,数据较小,而d[23]的最大值才是1000, 因此可以对d[23]的值从0到n枚举。 代码: #include<bits/stdc++.h> const int MAX=24; int d[26],num[25],r[25],n,tol,head[26],det,loca[10],anc[26]; bool bb,vis[26]; using namespace std; queue<int>P; struct node { int to,cost,next; }rode[3000]; void add(int a,int b,int c) { rode[tol].to=b; rode[tol].cost=c; rode[tol].next=head[a]; head[a]=tol++; } void addedge() { int i,j; for(i=8; i<24; i++) add(i-8,i,r[i]); for(i=0; i<23; i++) add(i,i+1,0); for(i=0; i<23; i++) add(i+1,i,-num[i+1]);//开始一直写成num[i],wa了好多次 for(i=0; i<24; i++) add(MAX,i,0); det=tol-1; add(0,MAX,-num[0]); for(i=0; i<8; i++) { loca[i]=tol; add(i+16,i,r[i]-0); } } void init() { for(int i=0; i<=24; i++) d[i]=-1e9; d[MAX]=0; memset(vis,0,sizeof(vis)); memset(anc,0,sizeof(anc)); while(!P.empty()) P.pop(); } void spfa() { bb=0; int sum=0; while(!bb&&sum<=n) { bb=1; int i,j; init(); for(i=0; i<8; i++) rode[loca[i]].cost=r[i]-sum; //sum更新后要对与sum有关的点更新 rode[det].cost=sum;//d[23]-d[MAX]>=sum,不要忘记此次更新 P.push(MAX); vis[MAX]=1; anc[MAX]++; while(!P.empty()) { int v=P.front(); P.pop(); vis[v]=0; for(int i=head[v]; i!=-1; i=rode[i].next) { node e=rode[i]; if(d[e.to]<d[v]+e.cost) { d[e.to]=d[v]+e.cost; if(!vis[e.to]) { P.push(e.to); vis[e.to]=1; anc[e.to]++; if(anc[e.to]>tol) { bb=0; sum++; break;//一旦有负圈则跳出,更新sum。 } } } } if(!bb)break; } }//若没有负圈或sum>n则循环结束 if(bb)printf("%d\n",sum); else printf("No Solution\n"); } int main() { int T; scanf("%d",&T); while(T--) { int i,j; tol=0; for(i=0; i<24; i++) scanf("%d",&r[i]); scanf("%d",&n); memset(num,0,sizeof(num)); memset(head,-1,sizeof(head)); for(i=1; i<=n; i++) { int x; scanf("%d",&x); num[x]++; } addedge(); spfa(); } return 0; }