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来判断】

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=;
struct edge{
int to;
int len;
int next;
}EDGE[];
queue<int>pq;
int edge_cnt=,dist[],stk[],head[],n,in[],r[],num[];
void add(int x,int y,int z)
{
EDGE[edge_cnt].to=y;
EDGE[edge_cnt].next=head[x];
EDGE[edge_cnt].len=z;
head[x]=edge_cnt++;
}
bool spfa()
{
while(!pq.empty())
{
pq.pop();
}
memset(dist,-,sizeof(dist));
memset(stk,,sizeof(stk));
memset(in,,sizeof(in));
dist[]=;
pq.push();
in[]=;
while(!pq.empty())
{
int qwq=pq.front();pq.pop();
// cout << qwq << endl;
in[qwq]++;
if(in[qwq]>){
return false;
}
stk[qwq]=;
for(int i = head[qwq] ; i != - ; i = EDGE[i].next)
{
int v=EDGE[i].to;
// cout << dist[v]<<v<<endl;
if(dist[v]<dist[qwq]+EDGE[i].len)
{
dist[v]=dist[qwq]+EDGE[i].len;
if(!stk[v]){
stk[v]=;
pq.push(v);
}
}
}
}
return true;
}
bool check(int x)
{
memset(head,-,sizeof(head));
for(int i = ; i <= ; i++)
{
add(i-,i,);
add(i,i-,-num[i]);
if(i>=)
add(i-,i,r[i]);
else
add(i+,i,r[i]-x);
}
add(,,-x);
add(,,x);
return spfa();
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
for(int i = ; i <= ; i++)
{
scanf("%d",&r[i]);
}
scanf("%d",&n);
memset(num,,sizeof(num));
for(int i = ; i < n ; i++)
{
int x;
scanf("%d",&x);
num[x+]++;
}
int l=;int r=n;
while(l<=r)
{
int mid=(l+r)/;
if(check(mid))
{
r=mid-;
}
else
{
l=mid+;
}
}
if(l>n)printf("No Solution\n");
else printf("%d\n",l);
}
return ;
}

【HDOJ1529】【差分约束+SPFA+二分】的更多相关文章

  1. 【poj3169】【差分约束+spfa】

    题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...

  2. O - Layout(差分约束 + spfa)

    O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...

  3. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

  4. 【BZOJ】2330: [SCOI2011]糖果(差分约束+spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束运用了最短路中的三角形不等式,即d[v]<=d[u]+w(u, v),当然,最长 ...

  5. (简单) POJ 3169 Layout,差分约束+SPFA。

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  6. poj Layout 差分约束+SPFA

    题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...

  7. BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)

    BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...

  8. POJ-3159.Candies.(差分约束 + Spfa)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 40407   Accepted: 11367 Descri ...

  9. 图论分支-差分约束-SPFA系统

    据说差分约束有很多种,但是我学过的只有SPFA求差分: 我们知道,例如 A-B<=C,那么这就是一个差分约束. 比如说,著名的三角形差分约束,这个大家都是知道的,什么两边之差小于第三边啦,等等等 ...

随机推荐

  1. 基于Quartz.NET 实现可中断的任务(转)

    Quartz.NET 是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET 允许开发人员根据时间间隔(或天)来调度作业.它实现了作 ...

  2. Vue + Element UI 实现权限管理系统(第三方图标库)

    使用第三方图标库 用过Elment的同鞋都知道,Element UI提供的字体图符少之又少,实在是不够用啊,幸好现在有不少丰富的第三方图标库可用,引入也不会很麻烦. Font Awesome Font ...

  3. Jquery源码探索

    封装原理 这里参考的jquery来进行封装的一个常用方法的一个库,可作为自己的一个库 原理:创建一个构造函数,将所有方法放在该构造函数原型里,访问$()方法时,返回这个构造函数的实例化,这样就简单的实 ...

  4. java串口编程

    报错:no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDrive java.lang.Unsatisfie ...

  5. oracle中的对象创建及删除语句【原创】

    oracle对象 1.表 a)创建表1 create table students( id number(10), stuno number(10) , sex varchar2(2), age in ...

  6. zookeeper集群环境搭建(使用kafka的zookeeper搭建zk集群)

    ---恢复内容开始--- 使用kafka的zookeeper来搞集群的话和单纯用zk的其实差不了多少. 0.说在前头,搭建kafka集群之前请把每个服务器的jdk搞起来. 1.安装kafka wget ...

  7. day32 信号量 事件 管道 进程池

    今日主要内容: 1.管道(Pipe) 数据接收一次就没有了 2.事件(Event) 3.基于事件的进程通信 4.信号量(Semaphore) 5. 进程池(重点) 6.进程池的同步方法和异步方法 7. ...

  8. 不同生产商的CPU以及大端/小端对齐

    ● 不同生产商的CPU以及大端/小端对齐 ※ ARM.AMD.Atom和intel之间的关系   intel公司和AMD公司生产的是相同的x86架构的CPU,这种CPU属于CISC(Complex I ...

  9. 理解K系列与ultra-scale的区别

    总结:   K系列FPGA与KU系列FPGA的主要区别,体现在: (1)工艺制程不一样,K-28nm,KU-20nm:   (2)Ultra-Scale采用SSI:大容量K系列也采用SSI,SSI为了 ...

  10. DevExpress WinForms使用教程:Diagram Control

    [DevExpress WinForms v18.2下载] DevExpress WinForms v18.2包含WinForms和WPF Diagram Controls的三个高要求功能:新的Dia ...