【POJ 1275】 Cashier Employment(差分约束系统的建立和求解)

Cashier Employment
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7569   Accepted: 2856

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

Source

做到如今为止 做的最麻烦的一个差分约束……

麻烦不是麻烦在求解上。而是建图……而且绝大多数差分约束的难点。都是建图。。毕竟求解就是个裸最短路

这题题意是 一个商店要招聘员工 商店有一个客流表 表示0~23点每点须要的最少员工数

之后一个整数n 表示有n个应聘员工

后面n行 每行一个0~23的整数 表示第i个员工工作的起始时间 每一个员工工作8小时

题目分析完成 条件不多 所以这就是这题的难点……要挖掘非常多隐含条件

设t[i]为商店第i小时须要的员工数 r[i]表示第i小时来应聘的员工数 数组S[i]表示0~i小时总共应聘的员工数

第一个条件 0 <= S[i]-S[i-1] <= r[i]

第二个条件 S[i]-S[i-8] >= t[i] (i-1时i-8招聘的员工刚好下班 因此S[i]-S[i-8]即为第i小时招聘的所有员工)

第三个条件 S[24]-S[0] <= sum(因为要找最少的总招聘人数,所以这里出现了一个未知量,而这个问题就转化成了求最小sum)

把三个问题化成同号 再细化一下 就变成了

S[i-1]-S[i] <= 0

S[i]-S[i-1] <= r[i]

S[i-8]-S[i] <= -t[i](0 < i <= 8)

S[i+16]-S[i] <= sum-t[i](8 < i <= 24)

S[24]-S[0] <= sum

找到不等关系就好办一点了 对sum二分 每次更新与sum有关的边权 然后跑最短路 推断一下能不能跑出来(有无负环)

代码例如以下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout) using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const double eps = 1e-8; struct Edge
{
int u,v,w;
}; Edge eg[233333];
int w[25],r[25];
int dis[25];
int tp; bool bellman()
{
memset(dis,INF,sizeof(dis));
dis[0] = 0; for(int i = 0; i <= 24; ++i)
for(int j = 0; j < tp; ++j)
if(dis[eg[j].v] > dis[eg[j].u]+eg[j].w)
dis[eg[j].v] = dis[eg[j].u]+eg[j].w; //判负环
for(int j = 0; j < tp; ++j)
if(dis[eg[j].v] > dis[eg[j].u]+eg[j].w)
return false; return true;
} void Add(int u,int v,int w)
{
eg[tp].u = u;
eg[tp].v = v;
eg[tp++].w = w;
} //重建与sum有关的边
void buildgraph(int sum)
{
tp = 64;
for(int i = 1; i < 9; ++i)
Add(i,i+16,sum-w[i]);
Add(24,0,-sum);
} int main()
{
int t,x,n,low,high; scanf("%d",&t);
while(t--)
{
tp = 0; high = 0;
for(int i = 1; i <= 24; ++i)
scanf("%d",&w[i]); memset(r,0,sizeof(r));
scanf("%d",&n);
high = n; while(n--)
{
scanf("%d",&x);
r[x+1]++;
} //边权不变的边(与sum无关的边)
for(int i = 1; i <= 24; ++i)
{
Add(i-1,i,r[i]);
Add(i,i-1,0);
if(i >= 9) Add(i,i-8,-w[i]);
} low = 0;
int ans = INF;
while(low <= high)
{
int mid = (low+high)>>1; buildgraph(mid);
if(bellman())
{
ans = mid;
high = mid-1;
}else low = mid+1;
} if(ans == INF) puts("No Solution");
else printf("%d\n",ans);
} return 0;
}

id=1275">

【POJ 1275】 Cashier Employment(差分约束系统的建立和求解)的更多相关文章

  1. 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 n ...

  2. [HDU 1529]Cashier Employment(差分约束系统)

    [HDU 1529]Cashier Employment(差分约束系统) 题面 有一个超市,在24小时对员工都有一定需求量,表示为\(r_i\),意思为在i这个时间至少要有i个员工,现在有n个员工来应 ...

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

    Cashier Employment Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7651   Accepted: 288 ...

  4. POJ 1275 Cashier Employment 挺难的差分约束题

    http://poj.org/problem?id=1275 题目大意: 一商店二十四小时营业,但每个时间段需求的雇员数不同(已知,设为R[i]),现有n个人申请这份工作,其可以从固定时间t连续工作八 ...

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

    Cashier Employment Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7997   Accepted: 305 ...

  6. POJ 1275 Cashier Employment(差分约束)

    http://poj.org/problem?id=1275 题意 : 一家24小时营业的超市,要雇出纳员,需要求出超市每天不同时段需要的出纳员数,午夜只需一小批,下午需要多些,希望雇最少的人,给出每 ...

  7. poj 1275 Cashier Employment

    http://poj.org/problem?id=1275 #include <cstdio> #include <cstring> #include <algorit ...

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

    题目地址:id=1275">POJ 1275 题意: 给出一个超市24小时各须要R[i]个雇员工作,有N个雇员能够雇佣.他们開始工作时间分别为A[i],求须要的最少的雇员人数. 思路: ...

  9. 【POJ1275】Cashier Employment 差分约束

    [POJ1275]Cashier Employment 题意: 超市经历已经提供一天里每一小时需要出纳员的最少数量————R(0),R(1),...,R(23).R(0)表示从午夜到凌晨1:00所需要 ...

随机推荐

  1. bzoj3994: [SDOI2015]约数个数和(反演+结论?!)

    这题做的历程堪称惊心动魄 刚刚学了莫比乌斯反演的我高高兴兴的和cbx一起反演式子 期间有突破,有停滞,有否定 然后苟蒻的我背着cbx偷偷打开了题解 看到了 我...... 去你的有个性质啊(当然还是自 ...

  2. CAD使用GetAllAppName读所有名称(com接口)

    主要用到函数说明: MxDrawEntity::GetAllAppName 得到所有扩展数据名称,详细说明如下: 参数 说明 [out, retval] IMxDrawResbuf** ppRet 返 ...

  3. phpstorm破解激活码

    一.将“0.0.0.0 account.jetbrains.com”添加到hosts文件中 二.浏览器打开 http://idea.lanyus.com,点击页面中的“获得注册码”,然后在注册时切换至 ...

  4. Python:socket实现ftp程序

    刚开始学习socket编程,还不是特熟练,码了好长时间,中间遇到许多问题,记录一下用socketserver写ftp server端: #!/usr/bin/env python import soc ...

  5. prop 和 attr 中一些羞羞的事情

    引言 前几天做一个迷你京东小项目的时候涉及到一个全选的小功能,一开始用的是 attr,但是效果完全不是自己想要的,当商品按钮点击过一次后,attr就无法对其状态进行更改,最后谷歌了一番发现需要用 pr ...

  6. jupyter 教程

    官网: http://jupyter.org/

  7. Python之套接字

    Python之套接字 客户端Client/服务端Server架构: 1.硬件C/S架构 2.软件C/S架构 OSI4层:4层里有五层,五层里又有7层. 四层---------五层--------七层 ...

  8. python面向对象编程实例

    1.编写程序, 编写一个学生类, 要求有一个计数器的属性, 统计总共实例化了多少个学生 class Student: """学生类""" c ...

  9. js变量类型详解

    <html> <title>js变量类型详解</title> <meta http-equiv="content-type" conten ...

  10. hihoCoder#1114 小Hi小Ho的惊天大作战:扫雷·一

    原题地址 回溯+搜索 枚举每个位置上能否放地雷,当第i个位置枚举完成后,第i-1个位置的情况就确定了,此时,检查第i-1个位置是否满足要求,即左右间隔为1的范围内地雷数是否等于申明数字,如果满足条件, ...