zoj1420Cashier Employment【差分约束系统论文题】

来源:互联网 发布:linux压缩文件命令 编辑:程序博客网 时间:2024/06/10 19:25

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: Asia 2000, Tehran (Iran)

事实证明,大多数论文只能用来借鉴,其一是pascal看不懂,其二是写得太精炼,智商不够用,这个题还告诉我们一道理,手懒用stl的前提是能用其他方法优化时间,比如二分

正文:本题据说是一个很难的差分约束了,而且我又读错题了QAQ,应该说是读少了条件,题中不仅要求了每个时间的人数,对于来应聘工作的人数也有了限制,单纯的我并没有看到后者,导致差点写成dp,不过为什么不能用网络流呢?大概是因为太麻烦了吧,太容易超时了==而且我的另一个错误是一位题中只让求一天的,其实是要求每天循环的情况

既然是差分约束就要努力的找不等关系,写不等式 。s[]数组表示共雇佣了的人数,num[] 当前时刻最多可雇佣人数,r[]某时刻的最少人数。1. s[i]-s[i-1]>=0 2. s[i-1]-s[i]>=-num[i]  (1<=i<=24)  3. s[i]-s[i-8]>=r[i](8<=i<=24) 4. s[i]-s[i+16]>=r[i]-ans  (1<=i<=8)   5.  s[24]-s[0]>=ans  后两个我自己写肯定得忘==其中ans是最终答案即s[24],二分找出即可

/*******************zoj14202016.3.218840C++ (g++ 4.7.2)*******************/#include <iostream>#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;const int inf=0x3f3f3f3f;const int maxn=50005;struct Edge{    int v,cost;    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}};vector<Edge>E[maxn];void addedge(int u,int v,int w){    E[u].push_back(Edge(v,w));}bool vis[maxn];int cnt[maxn];int dist[maxn];bool spfa(int start,int n){    memset(vis,false,sizeof(vis));    memset(dist,-inf,sizeof(dist));    vis[start]=true;    dist[start]=0;    queue<int>que;    while(!que.empty()) que.pop();    que.push(start);    memset(cnt,0,sizeof(cnt));    cnt[start]=1;    while(!que.empty())    {        int u=que.front();        que.pop();        vis[u]=false;        for(int i=0;i<E[u].size();i++)        {            int v=E[u][i].v;            if(dist[v]<dist[u]+E[u][i].cost)            {                dist[v]=dist[u]+E[u][i].cost;                if(!vis[v])                {                    vis[v]=true;                    que.push(v);                    if(++cnt[v]>n) return false;                }            }        }    }    return true;}int num[30],r[30];int main(){    //freopen("cin.txt","r",stdin);    int t,ans,m;    scanf("%d",&t);    while(t--)    {        for(int i=1;i<=24;i++) scanf("%d",&r[i]);        for(int i=0;i<25;i++) num[i]=0;        scanf("%d",&m);        for(int i=0;i<m;i++)        {            int tmp;            scanf("%d",&tmp);            num[tmp+1]++;///!!!        }        ans=1;        bool flag=true;        int l=0,right=m;        while(l<right)        {            for(int i=0;i<=24;i++) E[i].clear();            ans=(l+right)/2;            for(int i=1;i<=24;i++)            {                addedge(i-1,i,0);                addedge(i,i-1,-num[i]);            }            for(int i=8;i<=24;i++) addedge(i-8,i,r[i]);            for(int i=1;i<=8;i++) addedge(i+16,i,r[i]-ans);            addedge(0,24,ans);            if(spfa(0,25)) right=ans,flag=false;            else l=ans+1;        }        if(!flag) printf("%d\n",right);        else printf("No Solution\n");    }    return 0;}


0 0
原创粉丝点击