【HDOJ1529】【差分约束+SPFA+二分】

http://acm.hdu.edu.cn/showproblem.php?pid=1529

Cashier Employment

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1489    Accepted Submission(s): 672

Problem 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

 题目大意:一个超市,24小时营业,不同时间点所需的售货员数目不同,给出24小时各个时间的所需人数以及每个售货员的上班时间安排,每个售货员的工作时间是8小时,求最少需要多少售货员。

题目分析:由题目易知是道差分约束问题。则需要列出隐藏的不等式。

令S【I】表示【0,I】时间段以及工作过或者正在工作的人数【题目所求也就是S【24】】

令num【I】表示I时刻刚好开始工作的人数【也就是将题目中所说的每个售货员上班开始时间转化为了某一时刻刚好上班的人数】   

令R【I】表示时刻I所需人数  

则 S【I+1】-S【I】>= 0&&S【I+1】-S【I】<= num【I】【条件不等式一--->由每一时刻加入工作人数为num【I】来确定】

同时按照要求有    当 I >= 8 时 S【I】-S【I-8】>=R【I】

        当    I < 8 时 S【24】- S【I+16】+ S【I】> R【I】 【条件不等式二、三---->由每个时刻所需人数R【I】确定】

而最容易被遗忘的不等式是   S【24】-S【0】==  枚举值 mid,这是一个等式,化成不等式形式是 S【24】-S【0】>= 0 &&S【24】-S【0】<= 0 【不等式四、五-->由等式转化】 

【PS:记得初始化多个数组,不然WA到怀疑人生..】

二分S【24】的值,二分判断的条件就是连边之后能够得到最长路【即不存在正环,这一点可以通过SPFA来判断】

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<cstring>
  5 #include<queue>
  6 using namespace std;
  7 const int INF=100005;
  8 struct edge{
  9     int to;
 10     int len;
 11     int next;
 12 }EDGE[200005];
 13 queue<int>pq;
 14 int edge_cnt=0,dist[50006],stk[50006],head[50006],n,in[100],r[100],num[100]; 
 15 void add(int x,int y,int z)
 16 {
 17     EDGE[edge_cnt].to=y;
 18     EDGE[edge_cnt].next=head[x];
 19     EDGE[edge_cnt].len=z;
 20     head[x]=edge_cnt++;
 21 }
 22 bool spfa()
 23 {
 24     while(!pq.empty())
 25     {
 26         pq.pop();
 27     }
 28     memset(dist,-1,sizeof(dist));
 29     memset(stk,0,sizeof(stk));
 30     memset(in,0,sizeof(in));
 31     dist[0]=0;
 32     pq.push(0);
 33     in[0]=1;
 34     while(!pq.empty())
 35     {
 36         int qwq=pq.front();pq.pop();
 37 //        cout << qwq << endl;
 38         in[qwq]++;
 39         if(in[qwq]>25){
 40             return false;
 41         }
 42         stk[qwq]=0;
 43         for(int i = head[qwq] ; i != -1 ; i = EDGE[i].next)
 44         {
 45             int v=EDGE[i].to;
 46         //    cout << dist[v]<<v<<endl;
 47             if(dist[v]<dist[qwq]+EDGE[i].len)
 48             {
 49                 dist[v]=dist[qwq]+EDGE[i].len;
 50                 if(!stk[v]){
 51                 stk[v]=1;
 52                 pq.push(v);
 53             }            
 54             }
 55         }
 56     }
 57     return true;
 58 }
 59 bool check(int x)
 60 {
 61     memset(head,-1,sizeof(head));
 62     for(int i = 1 ; i <= 24 ; i++)
 63     {
 64         add(i-1,i,0);
 65         add(i,i-1,-num[i]);
 66         if(i>=8)
 67         add(i-8,i,r[i]);
 68         else
 69         add(i+16,i,r[i]-x);
 70     }
 71     add(24,0,-x);
 72     add(0,24,x);
 73     return spfa();
 74 }
 75 int main()
 76 {
 77     int t;
 78     scanf("%d",&t);
 79     while(t--)
 80     {
 81         for(int i = 1 ; i <= 24 ; i++)
 82         {
 83             scanf("%d",&r[i]);
 84         }
 85         scanf("%d",&n);
 86         memset(num,0,sizeof(num));
 87         for(int i = 0 ; i < n ; i++)
 88         {
 89             int x;
 90             scanf("%d",&x);
 91             num[x+1]++;
 92         }
 93         int l=0;int r=n; 
 94         while(l<=r)
 95         {
 96             int mid=(l+r)/2;
 97             if(check(mid))
 98             {
 99                 r=mid-1;
100             }
101             else
102             {
103                 l=mid+1;
104             }
105         }
106         if(l>n)printf("No Solution\n");
107         else printf("%d\n",l);
108     }
109     return 0;
110 }

 

转载于:https://www.cnblogs.com/MekakuCityActor/p/9021096.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值