【POJ1275】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

题解
为避免负数,时间计数1~24。令:
R[i] i时间需要的人数 (1<=i<=24)
T[i] i时间应聘的人数 (1<=i<=24)
x[i] i时间录用的人数 (0<=i<=24),其中令x[0]=0
再设s[i]=x[0]+x[1]+……+x[i] (0<=i<=24),
由题意,可得如下方程组:
(1) s[i]-s[i-8]>=R[i]        (8<=i<=24)
(2) s[i]-s[16+i]>=R[i]-s[24] (1<=i<=7)
(3) s[i]-s[i-1]>=0           (1<=i<=24)
(4) s[i-1]-s[i]>=-T[i]       (1<=i<=24)
这个差分约束有个特殊的地方,(2)的右边有未知数s[24]。
这时可以通过枚举s[24]=ans来判断是否有可行解。
即(2)变形为(2') s[i]-s[16+i]>=R[i]-ans (1<=i<=7)
再通过SPFA求解(1)(2')(3)(4)。

不过最后有可能出现这种情况:
(1)(2')(3)(4)虽然有解,但求出的s[24]小于代入(2')里的ans!
这时,显然得到的s[]不满足原来的(2)了(请仔细比较(2)与(2'))。
不过虽然得到的解不满足原方程组,但这并不代表(1)(2)(3)(4)在s[24]=ans时没有可行解!
此外,值得注意的是,当得到的s[24]>ans时,虽然s[24]不一定是最优解,但把ans置成s[24]后,确实是可行解。

所以,简单把(2)置换成(2')是有问题的!
为了等价原命题,必须再加上条件:s[24]>=ans
这就是所谓加出来的那条边(5) s[24]-s[0]>=ans
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
queue<int >q;
const int maxn=10000;
int pre[maxn],w[maxn],last[maxn],other[maxn],num1;
int r[30],num[maxn],dis[maxn],n,cnt[maxn];
bool vis[maxn];
void add(int x,int y,int z){
	num1++;
	pre[num1]=last[x];
	last[x]=num1;
	other[num1]=y;
	w[num1]=z;
}
inline void build(int ans){
	memset(last,0,sizeof(last));
	memset(w,0,sizeof(w));
	memset(pre,0,sizeof pre);
	num1=0;
	add(0,24,ans);
	for(int i=1;i<=24;i++)
	add(i-1,i,0),add(i,i-1,-num[i]);
	for(int i=9;i<=24;i++)
	add(i-8,i,r[i]);
	for(int i=1;i<=8;i++)
	add(i+16,i,r[i]-ans);
	
/*	for(int j=0;j<=24;j++) 
	{
		for(int i=last[j];i;i=pre[i])
		cout<<other[i]<<" ";
		cout<<endl;
	}*/
	
}
bool spfa(int ans){
	memset(dis,128,sizeof dis);
	memset(vis,0,sizeof vis);
	memset(cnt,0,sizeof cnt);
	while(!q.empty()) q.pop();
	
	dis[0]=0;vis[0]=1;cnt[0]++;
	q.push(0);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=last[u];i;i=pre[i]){ 
			int v=other[i];
			if(dis[v]<dis[u]+w[i]){
				dis[v]=dis[u]+w[i];
				if(!vis[v]){
					vis[v]=1;
					q.push(v); 
					if(++cnt[v]>24) return 0;
				}	
				
			}
		}
	}
	//printf("dis[24]=%d\n",dis[24]);
	if(dis[24]==ans) return 1;
	else return 0;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		bool flag=0;
		for(int i=1;i<=24;i++){
			scanf("%d",&r[i]);
			num[i]=0; 
		}
		int k;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d",&k);
			num[k+1]++;
		}
		for(int i=0;i<=n;i++){
			//printf("i=%d\n",i);
			build(i);
			if(spfa(i)==1){
				printf("%d\n",i);
				flag=1;
				break;
			}
		}
		if(flag==0) printf("No Solution\n") ;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值