# POJ1275 Cashier Employment(差分约束)

 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9078 Accepted: 3515

## 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

#include<cstdio>
#include<queue>
#include<cstring>
#define INF 1e8+10
using namespace std;
const int MAXN=1e5+10;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++)
char buf[MAXN],*p1=buf,*p2=buf;
inline int read()
{
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int R[MAXN],N,pep[MAXN],dis[MAXN],vis[MAXN];
struct node
{
int u,v,w,nxt;
}edge[MAXN];
int head[MAXN],num=1;
inline void AddEdge(int x,int y,int z)
{
edge[num].u=x;
edge[num].v=y;
edge[num].w=z;
edge[num].nxt=head[x];
head[x]=num++;
}
void PRE()
{
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
num=1;
}
int SPFA(int val)
{
memset(dis,-0x7f,sizeof(dis));
queue<int>q;
dis[0]=0;
q.push(0);
while(q.size()!=0)
{
int p=q.front();q.pop();
vis[p]=0;
if(p==24&&dis[p]>val) return 0;
for(int i=head[p];i!=-1;i=edge[i].nxt)
{
if(dis[edge[i].v]<dis[p]+edge[i].w)
{
dis[edge[i].v]=dis[p]+edge[i].w;
if(!vis[edge[i].v])    vis[edge[i].v]=1,q.push(edge[i].v);
}
}
}
return dis[24]<=val?1:0;

}
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
int QWQ=read();
while(QWQ--)
{
memset(pep,0,sizeof(pep));
for(int i=0;i<=23;i++) R[i]=read();
N=read();
for(int i=1;i<=N;i++)
pep[read()]++;
int r=N+1,l=0;
int ans=INF;
while(l<=r)
{
PRE();
int mid=l+r>>1;
for(int i=0;i<=23;i++) AddEdge(i,i+1,0),AddEdge(i+1,i,-pep[i]);
for(int i=7;i<=23;i++) AddEdge(i-7,i+1,R[i]);
AddEdge(0,24,mid);AddEdge(24,0,-mid);
for(int i=0;i<7;i++) AddEdge(i+17,i+1,R[i]-mid);
if(SPFA(mid)) ans=mid,r=mid-1;
else l=mid+1;
}
if(ans>N) printf("No Solution\n");
else printf("%d\n",ans);
}
return 0;
}

posted @ 2018-03-03 13:45 自为风月马前卒 阅读(...) 评论(...) 编辑 收藏

……