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. DMA-总结

    概念DMA “Direct Memory Access(存储器直接访问).这是指一种高速的数据传输操作,允许在外部设备和存储器之间直接读写数据.整个数据传输操作在一个称为"DMA控制器&qu ...

  2. u-boot的内存分布

    cpu会自动从NAND flash 中读取前4KB的数据放置在片内SRAM里(s3c2440是soc),同时把这段片内SRAM映射到nGCS0片选的空间(即0x00000000).cpu是从0x000 ...

  3. Vue + Element UI 实现权限管理系统 (管理应用状态)

    使用 Vuex 管理应用状态 1. 引入背景 像先前我们是有导航菜单栏收缩和展开功能的,但是因为组件封装的原因,隐藏按钮在头部组件,而导航菜单在导航菜单组件,这样就涉及到了组件收缩状态的共享问题.收缩 ...

  4. Ubuntu 16.04 中安装谷歌 Chrome 浏览器

    http://jingyan.baidu.com/article/335530da98061b19cb41c31d.html 根据教程安装成功!! http://askubuntu.com/quest ...

  5. Fiddler系列教程2:手机抓包图文教程

    上篇Fiddler教程,我们教了大家Fiddler安装配置及如何使用Fiddler进行基本的Http抓包及模拟请求,今天给大家介绍下如何使用Fiddler进行手机抓包. 运行环境为Windows 10 ...

  6. day2编程语言的两大分类

    编程的语言的发展经历了 机器语言 汇编语言 高级语言 高级语言更贴近人类的语言,但是必须被翻译成计算机能读懂的二进制后,才能够被执行,按照翻译方式分为 1   编译型(需要编译器,相当于用谷歌翻译); ...

  7. 遍历所有子物体中renderer(渲染器)中的material(材质)并改变其alpha值实现若隐若现的效果

    using UnityEngine;using System.Collections;using UnityEngine.UI; public class CubeControl : MonoBeha ...

  8. asp.net MVC之Action过滤器浅析

    在asp.net MVC中,Action过滤器是一大利器,它可以在以下两个步骤执行相关的代码: 1.执行Action方法之前:OnActionExecuting 2.Action方法执行完毕后:OnA ...

  9. IIS设置上传文件大小限制

    单位为字节. 500*1024*1024=524288000

  10. 从mysql读取数据写入mongo

    # coding:utf-8 # Created by qinlin.liu at 2017/3/14 import pymysql import datetime #pymongo说明文档  : h ...