图论(差分约束系统):POJ 1275 Cashier Employment

Cashier Employment

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7651   Accepted: 2886

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 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #define INF -1000000000
 5 using namespace std;
 6 int r[25],t[25];
 7 int cnt,fir[2010],nxt[2010],to[2010],val[2010];
 8 void addedge(int a,int b,int v)
 9 {
10     nxt[++cnt]=fir[a];
11     fir[a]=cnt;to[cnt]=b;
12     val[cnt]=v;
13 }
14 int n,ans;
15 int dis[30],in[30],vis[30],q[50010];
16 bool Spfa()
17 {
18     int S=0,f=1,b=1;
19     for(int i=0;i<=24;i++)
20         dis[i]=INF;
21     memset(in,0,sizeof(in));
22     memset(vis,0,sizeof(vis));
23
24     q[b++]=S;
25     dis[S]=0;
26     in[S]++;
27     vis[S]=1;
28     while(f<b){
29         int node=q[f++];vis[node]=false;
30         for(int i=fir[node];i;i=nxt[i])
31             if(dis[to[i]]<dis[node]+val[i]){
32                 dis[to[i]]=dis[node]+val[i];
33                 if(!vis[to[i]]){
34                     if(++in[to[i]]>n)
35                         return false;
36                     q[b++]=to[i];
37                     vis[to[i]]=true;
38                 }
39             }
40     }
41     return dis[24]==ans;
42 }
43
44 void Build()
45 {
46     memset(fir,0,sizeof(fir));cnt=0;
47     for(int i=8;i<=24;i++)
48         addedge(i-8,i,r[i]);
49
50     for(int i=1;i<=7;i++)
51         addedge(i+16,i,r[i]-ans);
52     for(int i=0;i<24;i++){
53         addedge(i+1,i,-t[i]);
54         addedge(i,i+1,0);
55     }
56     addedge(0,24,ans);
57 }
58
59 void Solve()
60 {
61     int r=0,h=n+1;
62     while(h-r>1)
63     {
64         ans=(r+h)>>1;
65         Build();
66         if(Spfa())
67             h=ans;
68         else
69             r=ans;
70     }
71     if(h==n+1)
72         printf("No Solution\n");
73     else
74         printf("%d\n",h);
75 }
76
77 int main(){
78     int T;int x;
79     scanf("%d",&T);
80     while(T--)
81     {
82         for(int i=1;i<=24;i++)
83             scanf("%d",&r[i]);
84         scanf("%d",&n);
85         memset(t,0,sizeof(t));
86         for(int i=1;i<=n;i++){
87
88             scanf("%d",&x);
89             ++t[x];
90         }
91         Solve();
92     }
93     return 0;
94 }    
  
时间: 2024-11-07 15:45:57

图论(差分约束系统):POJ 1275 Cashier Employment的相关文章

poj——1275 Cashier Employment 差分约束系统

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 di

poj 1275 Cashier Employment 差分约束

差分约束模板题,差分约束是判断联立不等式组是否有解的一种方法,建图是关键. 代码: //poj 1275 //sep9 #include <iostream> #include <queue> using namespace std; const int maxM=10024; const int maxN=32; struct Edge { int v,w,nxt; }edge[maxM]; int t[maxN],c[maxN],head[maxN],vis[maxN],inq

poj 1275 Cashier Employment - 差分约束 - 二分答案

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 d

(差分约束系统) poj 1201

Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22781   Accepted: 8613 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end po

(差分约束系统) poj 2983

Is the Information Reliable? Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 11676   Accepted: 3687 Description The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years ago. Draco established a line of defe

HDU [1529] || POJ [P1275] Cashier Employment

经典的差分约束+二分答案. 本题的难点在于如何建图. 设x[i] 表示第i个小时可以开始工作的有多少个人. num[i] 表示第i个小时最少需雇佣多少人. s[i] 表示1...i小时实际开始工作的有多少人 因为最后要求的是s[24]的最小值,所以我们以s为中心建图. 因为我们求得是最小值,所以都转化成>=号的形式 我们逐步分析题目的已知条件: 第i小时实际开始工作的人数应小于等于可以开始的人数,即 $ s[i]-s[i-1]<=x[i] $ 变成 $ s[i-1]-s[i] >=-x[

有关差分约束系统

差分约束系统详解(极力推荐)==> http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 个人瞎想 : 差分约束系统的题最重要的就是充分利用题目条件建立模型.构造出不等式最后使用最短路来算出答案,当然有些题目即使构造出了若干不等式我们可能只得到了部分约束性条件,真正的答案可能需要枚举或者二分枚举.在构造不等式的时候要充分考虑模型的实际意义,以防建少了边亦或者建多了边导致答案错误. 题目: POJ 3159 Candie

POJ1275 Cashier Employment[差分约束系统 || 单纯形法]

Cashier Employment Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7997   Accepted: 3054 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 hir

POJ 1275-Cashier Employment(差分约束系统)

题目地址:POJ 1275 题意: 给出一个超市24小时各须要R[i]个雇员工作,有N个雇员能够雇佣.他们開始工作时间分别为A[i],求须要的最少的雇员人数. 思路:这个题的查约束太多了!简直是差评!只是也不是否能定这是道好题. 设dis[i]为0-i小时内工作的人数(dis[24]即为所求).r[i]为第(i-1)-i小时时须要在工作的人数,t[i]能够在第i-1小时開始工作.能够建立起下面约束不等式: 0 <= dis[i]-dis[i-1] <= t[i];  1 <= i<