# POJ 1275--Cashier Employment【差分约束，经典建边】

Cashier Employment
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7279 Accepted: 2727

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

need[i]表示在第 i 个小时至少也要的人数，work[i]表示应聘者中可以在第i个小时开始工作的人数,s[i]表示前i个小时雇佣的人数，

x[ i ]表示第 i 个小时雇佣的人数。 s[i] - s[i - 1] = x[i]

（1） 0<= x[i] <= x[ i ] ,转化为 0 <= s[ i ] - s[i - 1] <= work[ i ]
（2） i >= 8 时：need[ i ] <= x[i] + x[i - 1] + x[i - 2] + x[i - 3] + x[i - 4] + x[i - 5] + x[i - 6] + x[i - 7]
转化为 need[ i ] <= s[ i ] - s[i - 8]
i < 8 时：s[ i ] +s[ 24 ] -s[16 + i] >= need[i] (不清楚的可以模拟一下)
（3）对上面的S[24]我们不知道它的值，但我们知道它表示前24个小时所雇用的总人数，也就是我们要求的结果sum.因此对于未知
的sum，我们需要从0到n枚举sum。需要再建一条边即：s[24] - s[0] >= sum

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#define maxn 100
#define INF 0x3f3f3f3f
using namespace std;

int N;//个应聘者
struct node {
int u, v, w, next;
};

node edge[200000];
bool vis[maxn];
int work[maxn];//work[i]表示在第 i 个小时开始工作的人数
int need[maxn];//应聘者中可以在第i个小时开始工作的人数

void init (){
cnt = 0;
}

void add(int u, int v, int w){
edge[cnt] = {u, v, w, head[u]};
}

void getmap(int sum){
for(int i = 1; i <= 24; ++i){
if(i >= 8)
else
add(16 + i, i, need[i] - sum);
}
}

int SPFA(int sum){
for(int i = 0; i <= 24; ++i){
dist[i] = -INF;
vis[i] = 0;
used[i] = 0;
}
dist[0] = 0;
vis[0] = 1;
used[0] = 1;
queue<int>q;
q.push(0);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = 0;
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].v;
int w = edge[i].w;
if(dist[v] < dist[u] + w){
dist[v] = dist[u] + w;
if(!vis[v]){
vis[v] = 1;
used[v]++;
if(used[v] > 24)
return 0;
q.push(v);
}
}
}
}
return dist[24] == sum;
}
int main (){
int T;
int OK, st;
scanf("%d", &T);
while(T--){
OK = 0;
memset(need, 0, sizeof(need));
memset(work, 0, sizeof(need));
for(int i = 1; i <= 24; ++i)
scanf("%d", &need[i]);
scanf("%d", &N);
for(int i = 0; i < N; ++i){
scanf("%d", &st);
work[st + 1]++;
}
for(int i = 0; i <= N; ++i){
init();
getmap(i);
if(SPFA(i)){
OK = 1;
printf("%d\n", i);
break;
}
}
if(!OK)
printf("No Solution\n");
}
return 0;
}