POJ-1275 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
 
 

题解:

这题差分约束系统建模有一定难度,令s[i]为0~i时间每个点雇佣的人总和,令s[24]-s[0] = sum(这里我们把每个时间+1让0成为源点),
我们可以通过每个时间点的人不能少于R构造出两个式子:s[i]-s[i-8] >= R[i] (i < 8) and s[i]+sum-s[16+i] >= R[i] (8 <= i <= 24)
又因为每个时间点可雇佣的人有限制a[i],即s[i]-s[i-1] <= a[i],且每个时间点雇佣的人肯定不是负数,则s[i]-s[i-1] >= 0,
构造出差分约束系统,然后枚举sum,得到sum的最小值,从小到大枚举500+ms,二分答案0ms或16ms。


代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<queue>
 6 using namespace std;
 7 #define INF 0x3f3f3f3f
 8 #define M(a, b) memset(a, b, sizeof(a))
 9 const int N = 30;
10 int d[N], a[N], R[N], cnt[N], m;
11 bool inq[N];
12 struct Node {
13     int to, w;
14 };
15 vector<Node> G[N];
16 
17 void AddEdge(int u, int v, int w) {
18     G[u].push_back(Node{v, w});
19 }
20 
21 bool spfa(int s) {
22     M(d, INF); M(cnt, 0); M(inq, 0);
23     d[s] = 0; cnt[s] = 1;
24     queue<int> q;
25     q.push(s); inq[s] = 1;
26     while (!q.empty()) {
27         int u = q.front(); q.pop();
28         inq[u] = 0;
29         for (int i = 0; i < G[u].size(); ++i) {
30             Node &e = G[u][i];
31             if (d[e.to] > d[u] + e.w) {
32                 d[e.to] = d[u] + e.w;
33                 if (inq[e.to]) continue;
34                 q.push(e.to); inq[e.to] = 1;
35                 if (++cnt[e.to] > 24) return false;
36             }
37         }
38     }
39     return true;
40 }
41 
42 void build(int sum) {
43     for (int i = 0; i <= 24; ++i) G[i].clear();
44     for (int i = 1; i <= 24; ++i)
45         AddEdge(i, i-1, 0), AddEdge(i-1, i, a[i]);
46     for (int i = 8; i <= 24; ++i) AddEdge(i, i-8, -R[i]);
47     for (int i = 1; i < 8; ++i) AddEdge(i, i+16, sum-R[i]);
48     AddEdge(24, 0, -sum);
49 }
50 
51 int main() {
52     int T, b;
53     scanf("%d", &T);
54     while (T--) {
55         M(a, 0);
56         for (int i = 1; i <= 24; ++i) scanf("%d", &R[i]);
57         scanf("%d", &m);
58         for (int i = 0; i < m; ++i) {
59             scanf("%d", &b);
60             a[b+1]++;
61         }
62         int l = 0, r = m, mid, ans = -1;
63         while (l < r) {
64             mid = (l + r) >> 1;
65             build(mid);
66             if (spfa(0)) ans = mid, r = mid;
67             else l = mid+1;
68         }
69         if (~ans) printf("%d\n", ans);
70         else printf("No Solution\n");
71     }
72 
73     return 0;
74 }

 

转载于:https://www.cnblogs.com/robin1998/p/6679417.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值