POJ 1275 Cashier Employment (差分约束 + spfa)
2016-05-01 15:40
323 查看
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
Sample Output
题意:已经提供你一天的每一小时需要出纳员的最少数量——R(0), R(1), ..., R(23)。
R(0)表示从午夜到上午1:00需要出纳员的最少数目,R(1)表示上午1:00
到2:00之间需要的,等等。以及知道应聘者开始工作的时间ti(连续 8 小时),
问最少需要请多少个出纳员。
分析:
设 have[i] 为来应聘的在第 i 个小时开始工作的人数
r[i] 为第 i 个小时至少需要的人数
x[i] 为招到的在第 i 个小时开始工作的人数
根据题意有:
0 <= x[i] <= have[i] //某个特定时间点招的人数最少为0,最多为应聘者人数
x[i-7] + x[i-6] + …+ x[i] >= r[i] (题目中的连续工作8小时)
再设 s[i] = x[1] + … + x[i]
则有: 由 0 <= x[i] <= have[i]
=> s[i] - s[i-1] >= 0
s[i-1] - s[i] >= -have[i]
由 x[i-7] + x[i-6] + …+ x[i] >= r[i]
=> s[i] - s[i-8] >= r[i] (8 <= i <= 24)
s[i] - s[i-8+24] >= r[i] - s[24] (1<= i <= 7)(代入理解)
还需要添加一个隐藏不等式: s[24] - s[0] >= ans(枚举的答案最小)
通过枚举 s[24],来检测是否满足条件,题目是求最小值,即求最长路,以0为源点。
注意也可以将不等式全部转化为 "<=" 形式,求的就是最短路而非最长路。
代码如下:
法一(直接从小到大找符合条件的答案)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7695 | Accepted: 2909 |
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
题意:已经提供你一天的每一小时需要出纳员的最少数量——R(0), R(1), ..., R(23)。
R(0)表示从午夜到上午1:00需要出纳员的最少数目,R(1)表示上午1:00
到2:00之间需要的,等等。以及知道应聘者开始工作的时间ti(连续 8 小时),
问最少需要请多少个出纳员。
分析:
设 have[i] 为来应聘的在第 i 个小时开始工作的人数
r[i] 为第 i 个小时至少需要的人数
x[i] 为招到的在第 i 个小时开始工作的人数
根据题意有:
0 <= x[i] <= have[i] //某个特定时间点招的人数最少为0,最多为应聘者人数
x[i-7] + x[i-6] + …+ x[i] >= r[i] (题目中的连续工作8小时)
再设 s[i] = x[1] + … + x[i]
则有: 由 0 <= x[i] <= have[i]
=> s[i] - s[i-1] >= 0
s[i-1] - s[i] >= -have[i]
由 x[i-7] + x[i-6] + …+ x[i] >= r[i]
=> s[i] - s[i-8] >= r[i] (8 <= i <= 24)
s[i] - s[i-8+24] >= r[i] - s[24] (1<= i <= 7)(代入理解)
还需要添加一个隐藏不等式: s[24] - s[0] >= ans(枚举的答案最小)
通过枚举 s[24],来检测是否满足条件,题目是求最小值,即求最长路,以0为源点。
注意也可以将不等式全部转化为 "<=" 形式,求的就是最短路而非最长路。
代码如下:
法一(直接从小到大找符合条件的答案)
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; #define N 1200 #define inf 0x3f3f3f3f int k, n, vis , dis , head , r , have , judge ; //s[i] <=> 第 1--i 小时工作的人数和 struct r { int u, v, w, next; }rr ; void add(int u, int v, int w) { rr[k].v = v; rr[k].w = w; rr[k].next = head[u]; head[u] = k++; } void build(int ans) { k = 0; memset(head, -1, sizeof(head)); add(0, 24, ans); for (int i = 1; i <= 24; i++){ add(i - 1, i, 0);//s[i]-s[i-1]>=0 add(i, i - 1, -have[i]);//s[i]-s[i-1]<=have[i] } for (int i = 1; i <= 7; i++) add(i + 16, i, r[i] - ans);//s[i]-s[i-8+24]>=r[i]-s[24] for (int i = 8; i <= 24; i++) add(i - 8, i, r[i]); //s[i] - s[i - 8] >= r[i](连续8个小时) } bool spfa(int ans) { int i, j, k; memset(vis, 0, sizeof(vis)); memset(judge, 0, sizeof(judge)); memset(dis, -inf, sizeof(dis)); queue<int >q; q.push(0); vis[0] = judge[0] = 1; dis[0] = 0; while (!q.empty()){ k = q.front(); q.pop(); vis[k] = 0; for (i = head[k]; i + 1; i = rr[i].next){ if (dis[rr[i].v] < dis[k] + rr[i].w){//求最长路 dis[rr[i].v] = dis[k] + rr[i].w; if (!vis[rr[i].v]){ q.push(rr[i].v); vis[rr[i].v] = 1; if (++judge[rr[i].v]>25) return false;//判环 } } } } if (dis[24] == ans) return true; return false; } int main() { #ifdef OFFLINE freopen("t.txt", "r", stdin); #endif int i, j, m, t, tt; scanf("%d", &t); while (t--){ for (i = 1; i <= 24; i++) scanf("%d", &r[i]); memset(have, 0, sizeof(have)); scanf("%d", &n); for (i = 0; i < n; i++){ scanf("%d", &tt); have[tt + 1]++;//从1开始 } bool ok = false; for (i = 0; i <= n; i++){ build(i); if (spfa(i)){ printf("%d\n", i); ok = true; break; } } if (!ok) puts("No Solution"); } return 0; }
法二(利用二分查找)
<pre name="code" class="cpp">#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; #define N 1200 #define inf 0x3f3f3f3f int k, n, vis , dis , head , r , have , judge ; //s[i] <=> 第 1--i 小时工作的人数和 struct r { int u, v, w, next; }rr ; void add(int u, int v, int w) { rr[k].v = v; rr[k].w = w; rr[k].next = head[u]; head[u] = k++; } void build(int ans) { k = 0; memset(head, -1, sizeof(head)); add(0, 24, ans); for (int i = 1; i <= 24; i++){ add(i - 1, i, 0);//s[i]-s[i-1]>=0 add(i, i - 1, -have[i]);//s[i]-s[i-1]<=have[i] } for (int i = 1; i <= 7; i++) add(i + 16, i, r[i] - ans);//s[i]-s[i-8+24]>=r[i]-s[24] for (int i = 8; i <= 24; i++) add(i - 8, i, r[i]); //s[i] - s[i - 8] >= r[i](连续8个小时) } bool spfa(int ans) { int i, j, k; memset(vis, 0, sizeof(vis)); memset(judge, 0, sizeof(judge)); memset(dis, -inf, sizeof(dis)); queue<int >q; q.push(0); vis[0] = judge[0] = 1; dis[0] = 0; while (!q.empty()){ k = q.front(); q.pop(); vis[k] = 0; for (i = head[k]; i + 1; i = rr[i].next){ if (dis[rr[i].v] < dis[k] + rr[i].w){//求最长路 dis[rr[i].v] = dis[k] + rr[i].w; if (!vis[rr[i].v]){ q.push(rr[i].v); vis[rr[i].v] = 1; if (++judge[rr[i].v]>25) return false;//判环 } } } } if (dis[24] == ans) return true; return false; } int binary_search(int l, int r){ int ok = -1; while (l <= r){ int mid = (l + r) >> 1; build(mid); if (spfa(mid)){// mid 表示一共能够雇佣的员工数量 ok = mid; r = mid - 1; //因为此题要求的是最小的雇佣人数,所以得判断 r=mid-1 是否满足题意,使之越来越小 } else l = mid + 1; } return ok; } int main() { #ifdef OFFLINE freopen("t.txt", "r", stdin); #endif int i, j, m, t, tt; scanf("%d", &t); while (t--){ for (i = 1; i <= 24; i++) scanf("%d", &r[i]); memset(have, 0, sizeof(have)); scanf("%d", &n); for (i = 0; i < n; i++){ scanf("%d", &tt); have[tt + 1]++; } if (binary_search(0, n) != -1) printf("%d\n", binary_search(0, n)); else puts("No Solution"); } return 0; }
相关文章推荐
- Ubuntu16.04 Sublime Text3 安装激活和设置
- 2016.5.1
- LeetCode 4. Median of Two Sorted Arrays(两个有序数组的中位数)
- 第 29 章 CSS3 弹性伸缩布局[上]
- 我的大学规划
- 读《Spring-技术内幕》-第二章:IoC容器的实现-1
- C语言编程程序的内存如何布局
- Genymotion 从注册到安装到运行到调试
- C++随笔
- PAT 1006. 换个格式输出整数 (15)
- 20145327 《Java程序设计》第九周学习总结
- day56-Spark SQL和DataFrame的本质
- 欢迎使用CSDN-markdown编辑器
- C 的布尔值
- PAT(Basic Level) 完美数列(25)
- apache+tomcat负载均衡
- 设置手机iphone5s邮件
- eclipse build workspace太慢或者 js出错问题解决
- Linux内核学习总结
- HTML学习小结