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

觉得好难分析...Orz 呜呜呜~ 还是自己太菜了

题解:这一题,我们可以用dis[i]表示从0到i总共雇佣的的Casher的数量,num[i]表示在i时正在工作的数量;sum[i]表示从0到i的需要工作时间的总时间;所以dis[23]即为所求,但是我们不能将23与0直接相连,会出现负环,故加一个汇点MAX=24指向0点,边权为     -dis[23];其实sum的最大值就是1000,也就1000中情况,故我们将sum从0~n意义枚举带入差分方程,如果有满足条件最长路则有解;

1.从8点到23点之间,满足dis[i]-dis[i-8]>=a[i];因为在i时,i-8的Casher已经停止工作了;

2.从0到22都满足 0=< sum[i+1]-sum[i] <=num[i+1];

3.在0到7点,由于可能含有昨天的剩余,则num[i]-num[i+16]>=a[i]-sum;

4.dis[i]-dis[MAX]>=0;

参考代码为:

#include<bits/stdc++.h>
using namespace std;
const int MAX=24;
const int INF=0x3f3f3f3f;
int T,n,x,a[24],temp[10],tot,tot2,sum;
int vis[25],dis[25],num[25],cnt[25],first[25];
struct Edge{
int to,w,net;
} edge[3000];
void addedge(int u,int v,int w)
{
edge[tot].to=v;
edge[tot].w =w;
edge[tot].net=first[u];
first[u]=tot++;
}
queue<int> q;
void Init()
{
memset(vis,0,sizeof vis);
memset(dis,-INF,sizeof dis);
memset(cnt,0,sizeof cnt);
while(!q.empty()) q.pop();
dis[MAX]=0;
} void build()
{
for(int i=8;i<24;i++) addedge(i-8,i,a[i]);
for(int i=0;i<23;i++) addedge(i,i+1,0);
for(int i=0;i<23;i++) addedge(i+1,i,-num[i+1]);
for(int i=0;i<24;i++) addedge(MAX,i,0);
tot2=tot-1;
addedge(0,MAX,-num[0]);
for(int i=0;i<8;i++)
{
temp[i]=tot;
addedge(i+16,i,a[i]-0);
}
} void SPFA()
{
bool flag=false;
while(!flag&&sum<=n)
{
flag=true;
Init();
for(int i=0;i<8;i++) edge[temp[i]].w=a[i]-sum;
edge[tot2].w=sum;
q.push(MAX); vis[MAX]=1; cnt[MAX]++;
while(!q.empty())
{
int u=q.front();q.pop();vis[u]=0;
for(int i=first[u];~i;i=edge[i].net)
{
if(dis[edge[i].to]<dis[u]+edge[i].w)
{
dis[edge[i].to]=dis[u]+edge[i].w;
if(!vis[edge[i].to])
{
vis[edge[i].to]=1;
q.push(edge[i].to);
if(++cnt[edge[i].to]>tot)
{
sum++;
flag=false;
break;
}
}
}
}
if(!flag) break;
}
}
if(flag) cout<<sum<<endl;
else cout<<"No Solution"<<endl;
} int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>T;
while(T--)
{
sum=tot=0;
memset(first,-1,sizeof first);
memset(num,0,sizeof num);
for(int i=0;i<24;i++) cin>>a[i];
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>x;
num[x]++;
}
build();
SPFA();
}
return 0;
}

HDU1529-Casher Emploryment(最最...最经典的差分约束 差分约束-最长路+将环变线)的更多相关文章

  1. hdu 1534(差分约束+spfa求最长路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...

  2. 社交APP经典死法18种,听野路子产品菜狗怎么说

    点这里 社交APP经典死法18种,听野路子产品菜狗怎么说 时间 2015-04-06 11:24:53  虎嗅网相似文章 (4)原文  http://www.huxiu.com/article/112 ...

  3. HDU.1529.Cashier Employment(差分约束 最长路SPFA)

    题目链接 \(Description\) 给定一天24h 每小时需要的员工数量Ri,有n个员工,已知每个员工开始工作的时间ti(ti∈[0,23]),每个员工会连续工作8h. 问能否满足一天的需求.若 ...

  4. NYOJ_矩形嵌套(DAG上的最长路 + 经典dp)

    本题大意:给定多个矩形的长和宽,让你判断最多能有几个矩形可以嵌套在一起,嵌套的条件为长和宽分别都小于另一个矩形的长和宽. 本题思路:其实这道题和之前做过的一道模版题数字三角形很相似,大体思路都一致,这 ...

  5. 洛谷P3275 [SCOI2011]糖果(差分约束,最长路,Tarjan,拓扑排序)

    洛谷题目传送门 差分约束模板题,等于双向连0边,小于等于单向连0边,小于单向连1边,我太蒻了,总喜欢正边权跑最长路...... 看遍了讨论版,我是真的不敢再入复杂度有点超级伪的SPFA的坑了 为了保证 ...

  6. 【bzoj3436】小K的农场 差分约束系统+最长路-Spfa

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801470.html 题目描述 背景 小K是个特么喜欢玩MC的孩纸... 描述 小K在MC里面建立很多很多的农场,总 ...

  7. POJ 1201 Intervals(差分约束 区间约束模版)

    关于差分约束详情可阅读:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 题意: 给定n个区间[L,R], 每个区间至 ...

  8. Spring经典高频面试题,原来是长这个样子

    Spring经典高频面试题,原来是长这个样子 2019年08月23日 15:01:32 博文视点 阅读数 719   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文 ...

  9. 洛谷5026 Lycanthropy 差分套差分

    题目链接 https://www.luogu.com.cn/problem/P5026 题意 在一个长度为m的序列中,每次给一个下标x,和一个权值v,然后从x-v*3到x-v*2单调递增,从x-v*2 ...

随机推荐

  1. Linux命令实战(一)

    1.pwd(printing working directory)打印当前工作目录路径 [root@test sysconfig]# pwd /etc/sysconfig 2.ls(list)列出当前 ...

  2. 微信APP支付【签名失败】

    最近在做微信APP支付 遇到一个问题 请求预下单时,接口返回签名错误 由于之前没有成功的交互,刚开始检查程序的错误,经过多次修改,发现依然是签名错误,可能出现的问题如下: 1.该签名密钥不是AppSe ...

  3. js常用的array方法

      1. splice() splice()方法向/从数组中添加/删除项目,然后返回被删除的项目.(注释:该方法会改变原始数组.) arrayObject.splice(index,howmany,i ...

  4. 手把手教你如何在阿里云ECS搭建Python TensorFlow Jupyter

    前段时间在阿里云买了一台服务器,准备部署网站,近期想玩一些深度学习项目,正好拿来用.TensorFlow官网的安装仅提及Ubuntu,但我的ECS操作系统是 CentOS 7.6 64位,搭建Pyth ...

  5. 关于RAID 10的介绍与创建

    一.RAID 10的简介 定义: RAID10也被称为镜象阵列条带.象RAID0一样,数据跨磁盘抽取:象RAID1一样,每个磁盘都有一个镜象磁盘, 所以RAID 10的另一种会说法是 RAID 0+1 ...

  6. BootStrap中的collapse插件堆叠效果

    通过网络上的一系列查找,总结出的collapse插件堆叠效果(网上没有找到,只能自己弄了,帮助那些和我遇到一样状况的同学) 首先感谢一位网友的知识总结给了我灵感,在这里先帮他推荐一波(https:// ...

  7. django_1:配置文件

    工程下: settings.py(建议设置成如下) DATABASES                                   #数据库配置 DEBUG = True           ...

  8. js 日常正则

    手机号 /^1((3[\d])|(4[5,6,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[1-3,5-8])|(9[1,8,9]))\d{8}$/ 大写字母 /^[A- ...

  9. LVM扩容之xfs文件系统

    LVM的基础请见:https://www.cnblogs.com/wxxjianchi/p/9698089.html 一.放大LV的容量.放大容量是由内而外来操作的. 1.设置新的lvm分区:用fdi ...

  10. Redis系列(四):Redis持久化和主从复制原理

    一.持久化 所谓的持久化就是把内存中的数据写到磁盘中去,防止服务宕机后内存数据丢失.Redis4.0之前提供了两种持久化方式:RDB(默认) 和AOF,Redis4.x之后新增了一种混合持久化(本文所 ...