HDU1529 Cashier Employment 题解 【差分约束】【二分答案】

来源:互联网 发布:linux grep命令返回值 编辑:程序博客网 时间:2024/06/01 08:59

【题目描述】
Problem 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.
大意:
德黑兰的某国营商店需要聘用一批员工,不同的时间有不同的人员需求,能够雇佣到得人数也不同。然而你一旦聘用了一个人,他就会跟着你干8个小时。输入需求与劳动力人数,输出满足需求最少需要的人数,不行就输出no solution。
【题解】
我们先申明三个数组:
sum[i]:表示第i时雇佣人得最小值
num[i]:i时可雇用得人数
r[i]:i时需要雇佣的人数
备注:i的范围是[1,24]而非[0,23],输入的时候要动脑筋
随后我们可以推出的不等式们:

sum[i]-sum[i-1]>=0;//i时雇佣的人数大于等于0sum[i]-sum[i-1]<=num[i]==>sum[i-1]-sum[i]>=num[i];//i时雇佣的人数不能超出市场能力sum[i]-sum[i-8]>=r[i];(r>=8&&r<=24)//i时雇佣的人数不能小于需求sum[24]+sum[i]-sum[i+16]>=r[i];(r<=7)sum[24]-sum[0]=0==>sum[24]-sum[0]>=0,sum[0]-sum[24]<=0;

之后我们以sum数组为中心建图,然而用于最后一个不等式里头有一个sum[24],这个东西是一个变量,三个变量是怎么都不能用差分约束解得。由此我们在外边二分一个sum[24],之后在check()里面搞差分约束就行了。
我们来看代码:

#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int N=10000;const int M=30;const int Q=1000;const int INF=1e9;struct edge{    int u,v,w;    int next;    edge(){next=-1;}}ed[2*N+5];int head[M+5],tot;int dis[M+5],flag[M+5],mark[N+5];queue<int> q;int n,t;int r[M+5],num[Q+5];//num[i]表示市场上i时可用的劳动力,r[i]表示第i时的需求,i属于[1,24] void build(int u,int v,int w){    ed[++tot].v=v;    ed[tot].w=w;    ed[tot].next=head[u];    head[u]=tot;}bool SPFA(int s,int e){    while(!q.empty())q.pop();    for(int i=0;i<=e;i++)    dis[i]=-INF;//最长路     memset(flag,0,sizeof(flag));    memset(mark,0,sizeof(mark));    dis[s]=0,mark[s]=1,flag[s]=1;    q.push(s);    while(!q.empty())    {        int u=q.front();        q.pop();        flag[u]=0;        for(int i=head[u];i!=-1;i=ed[i].next)        {            int v=ed[i].v;            if(dis[v]<dis[u]+ed[i].w)//最长路             {                dis[v]=dis[u]+ed[i].w;                if(!flag[v])                {                    flag[v]=1;                    if(++mark[v]>e)return false;//负环,显然不行                     q.push(v);                }            }        }    }    return true;}bool check(int x){    memset(head,-1,sizeof(head));    tot=0;    for(int i=1;i<=24;i++)    {        build(i-1,i,0);        build(i,i-1,-num[i]);        if(i>=8)build(i-8,i,r[i]);        else build(i+16,i,r[i]-x);    }    build(0,24,x);    build(24,0,-x);    return SPFA(0,24);}int main(){    scanf("%d",&t);    while(t--)    {        for(int i=1;i<=24;i++)        scanf("%d",&r[i]);        scanf("%d",&n);        memset(num,0,sizeof(num));        for(int i=1;i<=n;i++)        {            int x;            scanf("%d",&x);            num[x+1]++;//i属于[1,24]         }        int lf=0,rg=n,ans=-1;        while(rg>=lf)        {            int mid=(lf+rg)>>1;            if(check(mid)){ans=mid;rg=mid-1;}            else lf=mid+1;        }        if(ans==-1)printf("No Solution\n");        else printf("%d\n",ans);    }    return 0;}
阅读全文
1 0
原创粉丝点击