差分约束学习(二)POJ 1275:Cashier Employment

Cashier Employment
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7467 Accepted: 2812

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

题意是有一家店,给出了这家店每个时刻(从1到24时)所需要的店员数目,然后给出了N个应聘者,每个应聘者有相应的上岗时间,每个应聘者工作8个小时。问这家店最少可以雇多少个雇员,使得满足每一个时刻的要求。

t[i]表示第i小时可以上岗的应聘者人数。

r[i]表示第i小时需要的店员人数。

s[i]表示从1小时开始到第i小时雇的店员人数。这样s[i]-s[i-1]就可以表示第i个小时开始上岗的的店员人数。


这样就有0<=s[i]-s[i-1]<=t[i]

s[i+8]-s[i]>=r[i+8] (1=< i =< 16)这里建立的边就表示s[i] s[i+1] s[i+2] 一直到 s[i+8]这些开始上岗的店员人数都能照顾到第i+8个小时,这些人数加起来满足大于等于r[i+8]就OK。

当i>16时,同理有 (s[24]-s[i])+s[(i+8)%24]>=r[i]。而s[24]实际上就是总的雇的人数x。

最后再加上s[24]-s[0]>=x。

从小到大枚举x

代码:

#pragma warning(disable:4996)   
#include <iostream>  
#include <algorithm>  
#include <cmath>  
#include <vector>   
#include <string>   
#include <cstring>  
#include <queue> 
using namespace std;  

#define INF 0x7fffffff  
#define MAXN 2000

struct EDGE
{
	int v;
	int w;
	int next;
}edge[MAXN];

int N,wtf;
int head[25], d[25], vis[25],cnt[25];
int r[25], t[25], s[25];

void addedge(int u, int v, int w)
{
	edge[wtf].v = v;
	edge[wtf].w = w;
	edge[wtf].next = head[u];
	head[u] = wtf++;
}
bool SPFA(int vv)
{
	int x, i, e;
	for (i = 0; i <= 24; i++)
	{
		d[i] = -INF;
		cnt[i] = 0;
	}
	d[0] = 0;
	queue<int>q;
	q.push(0);

	while (!q.empty())
	{
		x = q.front();
		q.pop();
		vis[x] = 0;
		for (int e = head[x]; e != -1; e = edge[e].next)
		{
			if (d[edge[e].v] < d[x] + edge[e].w)
			{
				d[edge[e].v] = d[x] + edge[e].w;
				if (!vis[edge[e].v])
				{
					q.push(edge[e].v);
					vis[edge[e].v] = 1;

					cnt[edge[e].v]++;
					if (cnt[edge[e].v] > 24)
					{
						return false;
					}
				}
			}
		}
	}
	if (d[24] == vv)
		return true;
	else
		return false;
}

void build(int x)
{
	wtf = 0;
	memset(edge, 0, sizeof(edge));
	memset(head, -1, sizeof(head));
	memset(d, 0, sizeof(d));
	memset(vis, 0, sizeof(vis));
	memset(cnt, 0, sizeof(cnt));
	int i, j;
	for (i = 1; i <= 24; i++)
	{
		//addedge(i, i - 1, 0);
		//addedge(i - 1, i, -t[i]);

		addedge(i - 1, i, 0);
		addedge(i, i - 1, -t[i]);
	}
	addedge(0, 24, x);
	for (i = 1; i <= 24; i++)
	{
		if (i >= 1 && i < 8)
		{
			addedge(i+16, i, r[i] - x);
		}
		else
		{
			addedge(i-8, i, r[i]);
		}
	}
}

void read()
{
	int i, temp;
	for (i = 1; i <= 24; i++)
	{
		scanf("%d", &r[i]);
	}
	scanf("%d", &N);
	for (i = 1; i <= N; i++)
	{
		scanf("%d", &temp);
		t[temp + 1]++;
	}
}

void solve()
{
	int i;
	for (i = 0; i <= N; i++)//枚举申请者的个数
	{
		build(i);
		if (SPFA(i))
		{
			printf("%d\n", i);
			return;
		}
	}
	printf("No Solution\n");
	return ;
}

int main()
{ 	
	//freopen("i.txt", "r", stdin); 
	//freopen("o.txt", "w", stdout);
	
	int t;
	scanf("%d", &t);
	while (t--)
	{
		read();
		solve();
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值