POJ 1275 - Cashier Employment

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1275 - Cashier Employment相关的知识,希望对你有一定的参考价值。

?????????==   memset   ??????   ??????   ??????   lin   customers   specified   ??????   

 

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

 

Solution:

??????????????????????????????????????????????????????????????????????????????

?????????????????????????????????????????????????????????????????????????????????????????????R(i) (0 <= i < 24)?????????i???????????????i+1???????????????????????????????????????????????????N(N <= 1000)???????????????????????????????????????????????????????????????????????????????????? ti????????????i???????????????????????????????????????ti????????????????????????8?????????????????????????????????????????????????????????????????????????????????i????????????????????????????????????R(i)????????????????????????????????????????????? 

???????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????$s[i]$???????????????$i+1$????????????????????????????????????

????????????1?????????$i>=7$,???$s[i] - s[i-8] geq R[i]$???

????????????2?????????$0 leq i < 7$??????????????????$s[23] - s[i+16] + s[i] geq R[i]$???

????????????3????????????????????????????????????????????????????????????$i$????????????????????????????????????$b[i]$,??????????????????$i$???$0 leq s[i] - s[i-1] leq b[i]$??? 
????????????????????????????????????????????????????????????????????????$3$???$s$??????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????$s[23]$??????????????????????????????????????????????????????????????????$s[i] - s[i+16]geq R[i] - s[23]$,???????????????????????????????????????$s[23]$??????????????????????????????????????????????????????????????????????????????$s[23]$??????????????????????????????????????????$0$???$n$??????$s[23]$,?????????????????????????????????????????????????????????????????????????????????????????????????????????.

?????????????????????s$[-1] = 0$?????????????????????????????????????????????????????????????????????????????????$s[0] = 0$???

????????????????????????????????????$s[24]$????????????????????????????????????????????????????????????

?????????

 1 #include<bits/stdc++.h>
 2 #define il inline
 3 #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
 4 using namespace std;
 5 const int N=2005,M=26,inf=23333333;
 6 int T,n,a[M],dis[M],tot[M],net[N],to[N],cnt,h[M],w[N];
 7 bool vis[M];
 8 queue<int>q;
 9 
10 il int gi(){
11     int a=0;char x=getchar();
12     while(x<???0???||x>???9???)x=getchar();
13     while(x>=???0???&&x<=???9???)a=(a<<3)+(a<<1)+x-48,x=getchar();
14     return a;
15 }
16 
17 il void add(int u,int v,int c){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt,w[cnt]=c;}
18 
19 il void init(int x){
20     cnt=0;
21     memset(vis,0,sizeof(vis));
22     memset(h,0,sizeof(h));
23     memset(dis,-0x3f,sizeof(dis));
24     while(!q.empty())q.pop();
25     For(i,0,23) add(i,i+1,0),add(i+1,i,-tot[i]);
26     For(i,7,23) add(i-7,i+1,a[i]);
27     add(0,24,x),add(24,0,-x);
28     For(i,0,6) add(i+17,i+1,a[i]-x);
29 }
30 
31 il bool check(int x){
32     init(x);
33     q.push(0);dis[0]=0;
34     while(!q.empty()){
35         int u=q.front();q.pop();vis[u]=0;
36         if(u==24&&dis[u]>x)return 0;
37         for(int i=h[u];i;i=net[i])
38             if(dis[to[i]]<dis[u]+w[i]){
39                 dis[to[i]]=dis[u]+w[i];
40                 if(!vis[to[i]])q.push(to[i]),vis[to[i]]=1;
41             }
42     }
43     return dis[24]==x?1:0;
44 }
45 
46 il void solve(){
47     int l=0,r=n+1,mid,ans=inf;
48     while(l<=r){
49         mid=l+r>>1;
50         if(check(mid))r=mid-1,ans=mid;
51         else l=mid+1;
52     }
53     if(ans>n)printf("No Solution
");
54     else printf("%d
",ans);
55 }
56 
57 int main(){
58     T=gi();
59     while(T--){
60         memset(tot,0,sizeof(tot));
61         For(i,0,23) a[i]=gi();
62         n=gi();
63         int u;
64         For(i,1,n)u=gi(),tot[u]++;
65         solve();
66     }
67     return 0;
68 }

 

以上是关于POJ 1275 - Cashier Employment的主要内容,如果未能解决你的问题,请参考以下文章

图论(差分约束系统):POJ 1275 Cashier Employment

POJ 1275 - Cashier Employment

poj 1275 Cashier Employment - 差分约束 - 二分答案

POJ1275 Cashier Employment[差分约束系统 || 单纯形法]

POJ1275 Cashier Employment 二分 + 差分约束

POJ 1275-Cashier Employment(差分约束系统)