Cashier Employment poj1275(差分约束)

来源:互联网 发布:淘宝数据采集器免费 编辑:程序博客网 时间:2024/06/06 23:16
Cashier Employment
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 7936 Accepted: 3032

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

11 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1502322110

Sample Output

1
以下参考:http://blog.csdn.net/wangjian8006/article/details/7956356

题目大意:            德黑兰的一家每天24小时营业的超市,需要一批出纳员来满足它的需求。超市经理雇佣你来帮他解决一个问题————超市在每天的不同时段需要不同数目的出纳员(例如,午夜只需一小批,而下午则需要很多)来为顾客提供优质服务,他希望雇佣最少数目的纳员。            超市经历已经提供一天里每一小时需要出纳员的最少数量————R(0),R(1),...,R(23)。R(0)表示从午夜到凌晨1:00所需要出纳员的最少数目;R(1)表示凌晨1:00到2:00之间需要的;等等。每一天,这些数据都是相同的。有N人申请这项工作,每个申请者i在每天24小时当中,从一个特定的时刻开始连续工作恰好8小时。定义ti(0<=ti<=23)为上面提到的开始时刻,也就是说,如果第i个申请者被录用,他(或她)将从ti时刻开始连续工作8小时。            试着编写一个程序,输入R(i),i=0,...,23,以及ti,i=1,...,N,它们都是非负整数,计算为满足上述限制需要雇佣的最少出纳员数目、在每一时刻可以有比对应R(i)更多的出纳员在工作输入描述:            输入文件的第1行为一个整数T,表示输入文件中测试数据的数目(至多20个)。每个测试数据第一行为24个整数,表示R(0),R(1),...,R(23),R(i)最大可以取到1000。接下来一行是一个整数N,表示申请者的数目,0<=N<=1000。接下来有N行,每行为一个整数ti,0<=ti<=23,测试数据之间没有空行。输出描述:           对输入文件中的每个测试数据,输出占一行,为需要雇佣的出纳员的最少数目。如果某个测试数据没有解。则输出"No Solution"。注:题目描述来自于"图论算法理论、实现及应用"。

解题思路:

           这题太难了,想了半天没有一点思路,剩下半天找资料分析,下面是黑书————《算法艺术与信息学竞赛》

中的描述:

 

 

 还有一些:

 差分约束的关键是 要充分利用所给条件 建立数组的 逻辑意义 如此题的dis[]数组(s[]数组) 建立差分约束系统即(找出差分约束关系),    摘自冯威论文——《数与图的完美结合》   设num[ i ]为i时刻能够开始工作的人数,x[ i ]为 第 i 时刻实际雇佣的人数,那么x[ I ]<=num[ I ]。  设r[ i ](输入的每一时刻所需的人数)为i时刻至少需要工作的人数,于是有如下关系:      x[ I-7 ]+x[ I-6 ]+x[ I-5 ]+x[ I-4 ]+x[ I-3 ]+x[ I-2 ]+x[ I-1 ]+x[ I ]>=r[ I ] 设s[ I ]=x[ 1 ]+x[ 2 ]…+x[ I ],得到      0<=s[ I ]-s[ I-1 ]<=num[ I ], 0<=I<=23      s[ I ]-s[ I-8 ]>=r[ I ], 8<=I<=23    建立关系     s[ 23 ]+s[ I ]-s[ I+16 ]>=r[ I ], 0<=I<=7 建立可以循环的关系       对于以上的几组不等式,我们采用一种非常笨拙的办法处理这一系列的不等式(其实也是让零乱的式子变得更加整齐,易于处理)。首先我们要明白差分约束系统的应用对象(它通常针对多个二项相减的不等式的)于是我们将上面的所有式子都转化成两项未知项在左边,另外的常数项在右边,且中间用>=连接的式子,即:     s[ I ]-s[ I-1 ]>=0            (0<=I<=23)      s[ I-1 ]-s[ I ]>=-num[ I ]       (0<=I<=23)      s[ I ]-s[ I-8 ]>=r[ I ]         (8<=I<=23)      s[ I ]-s[ I+16 ]>=r[ I ]-s[ 23 ]  (0<=I<= 7)     论文有点小小的漏洞,也导致了它论文附带的程序是错误的,有BUG  可行方案中还差一个约束条件  S[23] - S[-1] >= sum;sum为雇佣的出纳员总数     将所有形如 A-B>=C 的式子 我们从节点B 引出一条有向边指向A边的权值为C  (这里注意由于左右确定,式子又是统一的>=的不等式,所以A和B是相对确定的,边是一定是指向A的) ,图就建成了 。  在程序中,我用0作为上面说的-1来构图

#include<stdio.h>#include<string.h>#define INF 0x3f3f3f3f#define maxn 10000struct node{    int u,w,v;}e[maxn];int t[maxn];int r[maxn];int s[maxn];int dis[maxn];int cnt;int m,ans,mid;void add(int u,int v,int w){    e[cnt].u=u;    e[cnt].v=v;    e[cnt++].w=w;}void build(int x){    int i;    cnt=48;    for(i=1;i<=8;i++)add(i,i+16,x-r[i]);    for(i=9;i<=24;i++)add(i,i-8,-r[i]);    add(24,0,-x);}int bellman(){    int i,j;    for(i=0;i<=24;i++)        dis[i]=INF;    dis[0]=0;    for(i=1;i<=24;i++)    {        for(j=0;j<cnt;j++)        {            int u=e[j].u;            int v=e[j].v;            int w=e[j].w;            if(dis[v]>dis[u]+w)                dis[v]=dis[u]+w;        }    }         for(j=0;j<cnt;j++)        {            int u=e[j].u;            int v=e[j].v;            int w=e[j].w;            if(dis[v]>dis[u]+w)            {                dis[v]=dis[u]+w;return 0;            }        }    return 1;}void erfen(int low,int high){    if(low>high) return;    mid=(low+high)/2;    build(mid);    if(bellman())    {        ans=mid;        erfen(low,mid-1);    }    else erfen(mid+1,high);}int main(){    int tmp;    scanf("%d",&tmp);    while(tmp--)    {        memset(t,0,sizeof(t));        int i;        cnt=0;        r[0]=0;        for(i=1;i<=24;i++)        {            scanf("%d",&r[i]);        }        int x;        scanf("%d",&m);        for(i=1;i<=m;i++)        {            scanf("%d",&x);            t[x+1]++;        }        for(i=1;i<=24;i++)        {            add(i-1,i,t[i]);            add(i,i-1,0);        }        int flat=0;        //printf("---\n");  //printf("%d\n",bellman());        ans=-1;        erfen(0,m);        if(ans==-1) printf("No Solution\n");        else printf("%d\n",ans);    }}


                                             
0 0
原创粉丝点击