Cashier Employment_poj1275_差分约束

来源:互联网 发布:手机淘宝联盟何时返利 编辑:程序博客网 时间:2024/06/06 11:43

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.

Analysis


挺难的吧,不看题解做不出来的那种
0sisi1
tisi1si
risisi8 i8
rianssisi+16 i<8
anss24
然后枚举ans的值去建图,spfa

Code


/*ID:wjp13241PROG:cahsierLANG:C++*/#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>#include <stack>#include <queue>#define fo(i,a,b) for (int i=a;i<=b;i++)#define fil(x,t) memset(x,t,sizeof(x))#define STP system("pause")#define min(x,y) x<y?x:y#define max(x,y) x>y?x:y#define ll long long#define INF 0x7f7f7f7f#define EPS 1e-4#define D 25#define E D*D*D+1using namespace std;struct edge{int y,w,next;}e[E];int r[D],s[D],cnt[D],dis[D],t[D],ls[E],maxE=0;bool vis[D];int add(int x,int y,int w){e[++maxE]=(edge){y,w,ls[x]},ls[x]=maxE;}int spfa(int st,int ed){    fil(vis,false);    fil(dis,-63);    fil(cnt,0);    vis[st]=true;    dis[st]=0;    queue<int>q;    q.push(st);    while (!q.empty())    {        int now=q.front();q.pop();        if (cnt[now]++>D)            return -1;        for (int i=ls[now];i;i=e[i].next)            if (e[i].w+dis[now]>dis[e[i].y])            {                dis[e[i].y]=dis[now]+e[i].w;                if (!vis[e[i].y])                {                    vis[e[i].y]=true;                    q.push(e[i].y);                }            }        vis[now]=false;    }    return dis[ed];}int main(){    ios::sync_with_stdio(false);    int T;    cin>>T;    while (T--)    {        fo(i,1,D-1)            cin>>r[i];        int n;        cin>>n;        fil(t,0);        fo(i,1,n)        {            int x;            cin>>x;            t[x+1]++;        }        bool flag=false;        fo(ans,0,n+1)        {            fil(ls,0);            add(0,24,ans);            fo(i,1,D-1)            {                add(i,i-1,-t[i]);                add(i-1,i,0);                if (i>8)                    add(i-8,i,r[i]);                else                    add(i+16,i,r[i]-ans);            }            if (spfa(0,24)==ans)            {                flag=true;                cout<<ans<<endl;                break;            }        }        if (!flag)            cout<<"No Solution"<<endl;    }    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 食管胃粘膜异位怎么办 狗狗肿瘤破了怎么办 婴儿胃食道反流怎么办 小儿胃食道反流怎么办 放疗后咳嗽痰多怎么办 胃息肉是恶性的怎么办 老是胃疼,胃胀怎么办 吃完饭之后胃胀怎么办 胃痛胃胀怎么办能缓解 胃胀不消化怎么办快速解决 便秘肛裂了好痛怎么办 胃消化慢还便秘怎么办 11个月婴儿便秘怎么办 80多岁老人便秘怎么办 狗狗便秘拉不出来怎么办 2个月幼犬便秘怎么办 狗狗便秘怎么办吃什么 痔疮又痛又痒怎么办 痔疮肉球特别痒怎么办 长了个小痔疮怎么办 产后4天没大便怎么办 7个月孕妇痔疮怎么办 运动完恶心想吐怎么办 跑步后恶心想吐怎么办 肠子不蠕动严重便秘怎么办 怀孕八个月严重便秘怎么办 怀孕七个月便秘严重怎么办 怀孕两个月便秘严重怎么办 3岁宝宝上火便秘怎么办 7个月的宝宝贫血怎么办 9个月婴儿贫血怎么办 肛裂大便有血怎么办 生完宝宝肛门痛怎么办 肛周脓肿出血了怎么办 胃胀怎么办简单的办法 吃多了胃胀难受怎么办 胃窦炎胆汁反流怎么办 怀孕总胆汁酸高怎么办 胃里胆汁反流怎么办 苦胆水吐出来了怎么办 喝多了吐胆汁怎么办