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
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 |
提示
题意:
有一家24小时营业的超市,将给出每整点所需要收银员的人数,之后大概有n(0<=n<=1000)个应聘者在0-23小时之间工作,工作持续时间是8个小时。超市每天所需的收银员人数不会变动(当做构成了时间环),为了节省成本,求所需招聘最少的收银员人数。如果不能达到超市的要求输出“No Solution”。
思路:
根据黑书中的介绍有以下不等式(这本书反正我没有,偷瞄了一会博客):
(图片来自:http://blog.csdn.net/wangjian8006/article/details/7956356)
-1呢就是超级源点,建边求最短路(最长路)就出来了,不要忘记还有负环(正环)的判断。
示例程序
Source Code
Problem: 1275 Code Length: 2078B
Memory: 356K Time: 32MS(16MS) //括号中为二分优化的时间复杂度
Language: GCC Result: Accepted
#include <stdio.h>
#include <string.h>
#define MAX 1000000007
struct
{
int v,d,next;
}w[100];
int h[100],numw,a[25],b[25];
void insert(int u,int v,int d)
{
w[numw].v=v;
w[numw].d=d;
w[numw].next=h[u];
h[u]=numw;
numw++;
}
void create(int sum)
{
int i;
insert(0,24,-sum);
for(i=1;24>=i;i++)
{
insert(i-1,i,0);
insert(i,i-1,b[i]);
}
for(i=17;24>=i;i++)
{
insert(i,(i+8)%24,-a[(i+8)%24]+sum);
}
for(i=1;16>=i;i++)
{
insert(i,i+8,-a[i+8]);
}
}
int spfa(int sum)
{
int pos,pos1,i,d[25],v[25],num[25],q[10000],f=0,top=0;
for(i=0;25>i;i++)
{
d[i]=MAX;
v[i]=0;
num[i]=0;
}
v[0]=1;
d[0]=0;
num[0]++;
q[top]=0;
top++;
while(f<top)
{
pos=q[f];
v[pos]=0;
for(i=h[pos];i!=-1;i=w[i].next)
{
pos1=w[i].v;
if(d[pos1]>d[pos]+w[i].d)
{
d[pos1]=d[pos]+w[i].d;
if(v[pos1]==0)
{
v[pos1]=1;
num[pos1]++;
if(num[pos1]>24) //还需判断负环
{
return 0;
}
q[top]=pos1;
top++;
}
}
}
f++;
}
if(d[24]==-sum)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int n,t,i,i1,x;
scanf("%d",&t);
for(i=1;t>=i;i++)
{
for(i1=1;24>=i1;i1++)
{
scanf("%d",&a[i1]);
b[i]=0;
}
scanf("%d",&n);
for(i1=1;n>=i1;i1++)
{
scanf("%d",&x);
b[x+1]++;
}
for(i1=0;n>=i1;i1++) //暴力枚举(可二分优化)
{
numw=0;
memset(h,-1,sizeof(h));
create(i1); //不等式建立边
if(spfa(i1)==1)
{
printf("%d\n",i1);
break;
}
}
if(i1>n)
{
printf("No Solution\n");
}
}
return 0;
}