Cashier Employment
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 3409 | Accepted: 1211 |
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
Source
刷进第一版了。。。。
这道题目黑书上有,很经典的一道查分约束系统的题目啊
主要是在不等式的推理上相当的麻烦,并且太容易遗漏了
先推出前面两个 S[I]表示从1到I共招收了几个收钱的员工
最简单的两个
s[i]-s[i-1]>=0
s[i]-s[i-1]<=r[i]
接着另外一个很容易推出来的
s[24]-s[0]>=sum
另外两个就麻烦了,思考一下整个过程需要的条件,那么就可以看出,要么是从尾巴到头,要么是在中间
从尾巴到头有两种情况,一个是我从尾巴到头,另一个是尾巴到头到了我
s[i]-s[j]>=r[i] i>j i=(j-1+8)%24+1 因为为了避免出现-1.我们都加了1,可能出现24
另外一个s[i]-s[j]>=r[i]-sum 即原来的s[j]-s[i]<=sum-r[i] i<j i=(j-1+8)%24+1
#include<stdio.h>
#include<string.h>
const int INF=1000000000;
struct EDGE
{
int u,v,val;
}edge[100];//因为有多个不等式关系,差不多是24+24+24+24+1
int r[25],t[25],dist[25],e,sum;
void addedge(int a,int b,int c)
{
edge[e].u=a;
edge[e].v=b;
edge[e++].val=c;
}
bool bellman()
{
for(int i=0;i<=24;i++) dist[i]=-INF;
dist[0]=0;
for(int i=0;i<24;i++)
{
bool flag=true;
for(int j=0;j<e;j++)
if(dist[edge[j].u]!=-INF&&dist[edge[j].v]<dist[edge[j].u]+edge[j].val)
{
dist[edge[j].v]=dist[edge[j].u]+edge[j].val;
flag=false;
}
if(flag) break;
}
for(int i=0;i<e;i++)
if(dist[edge[i].v]<dist[edge[i].u]+edge[i].val) return false;
return dist[24]==sum?true:false;
}
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
for(int i=1;i<=24;i++) scanf("%d",&r[i]);
scanf("%d",&n);
memset(t,0,sizeof(t));
for(int i=1;i<=n;i++)
{
int time;
scanf("%d",&time);
t[time+1]++;
}
e=0;
//必须加上0这个点,因为要从这个点出发,并且这个点的值为0 S[0]=0
for(int i=1;i<=24;i++)
{
addedge(i-1,i,0);//s[i+1]>=s[i]
addedge(i,i-1,-t[i]);//s[i-1]>=s[i]+(-t[i])
}
int tt=e;
bool flag=false;
for(sum=0;sum<=n;sum++)
{
e=tt;
addedge(0,24,sum);
for(int j=1;j<=24;j++)//必须从1到24是没有区别的
{
int i=(j+7)%24+1;//(j-1+8)%24+1,因为可能出现24并且I不会出现0
if(i>j) addedge(j,i,r[i]);
else addedge(j,i,r[i]-sum);
}
if(bellman())
{
printf("%d/n",sum);
flag=true;
break;
}
}
if(!flag) printf("No Solution/n");
}
return 0;
}