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;
}

最新文章

  1. linux命令执行返回值(附错误对照表)
  2. webForm练习1(地区导航)
  3. [转]查看手机已经记住的WIFI密码
  4. POJ2049Finding Nemo(bfs + 构图)
  5. uimodalpresentationformsheet resize ios7
  6. javascript中ajax post实例详解
  7. 十九、mysql 数据分布式
  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单
  9. hdu Buy the Ticket
  10. 手机摄像头扫描识别车牌号,移动端车牌识别sdk
  11. vue安装遇到vue不是内部变量
  12. 【转】FFmpeg获取DirectShow设备数据(摄像头,录屏)
  13. 使用ContentProvider实现多应用的数据共享
  14. React Native在window下的环境搭建(二):创建新项目
  15. python第一百零五天 ---Django 基础 路由系统 URL 模板语言 ORM 操作
  16. git bash 报错bash: *: command not found
  17. 树莓派进阶之路 (024) - windows远程桌面连接树莓派通过xrdp服务(转)
  18. java后台调用url
  19. postman—数据同步和创建测试集
  20. P3275 [SCOI2011]糖果 &amp;&amp; 差分约束(二)

热门文章

  1. Ubuntu编译安装HAprox+Keepalived+MySQL负载高可用架构(结合Docker容器配置)
  2. PowerMock学习(一)之PoweMock的入门--模拟新增学生操作
  3. Docker学习-Kubernetes - 集群部署
  4. redis 底层数据结构
  5. 【微信小程序】踩坑指南(持续更新)
  6. MyBatis批量插入模板
  7. H5 - css3(学习c3的第一天)
  8. C语言|博客作业07
  9. mysql基础之约束
  10. ceph中rbd的增量备份和恢复