2013
12-12

# Cashier Employment

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.

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.

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.

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

1

 X1 – X2 <= 0 X1 – X5 <= -1 X2 – X5 <= 1 X3 – X1 <= 5 X4 – X1 <= 4 X4 – X3 <= -1 X5 – X3 <= -3 X5 – X4 <= -3

 d(v) <= d(u) + w(u, v)

 X1 – X0 <= 0 X2 – X0 <= 0 X3 – X0 <= 0 X4 – X0 <= 0 X5 – X0 <= 0

 X0 = 0

 d(v) >= d(u) + w(u, v) 也就是 d(v) – d(u) >= w(u, v)

OK 上述纯属copy,有错误,请向原作者指出http://imlazy.ycool.com/post.1702305.html;

Bellman-Ford算法:

SPFA算法:

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 ]

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)

S[I]-S[start]>=0   (0<=I<=23)

S[End]-S[Start]=Ans(这个权值与大牛的最后个约束式的S[23]相当，并且在每次迭代中进行枚举)

#include <iostream>

#include <queue>

using namespace std;

const long MAXN=1000;

const long lmax=0xF000FFFF;

typedef struct

{

long v;

long next;

long cost;

}Edge;

Edge e[MAXN];

long p[MAXN];

long Dis[MAXN];

bool vist[MAXN];

long ct[MAXN];

long R[30];

long S[30];

long Num[30];

queue<long> q;

long eid;

inline void init()

{

memset(vist,0,sizeof(vist));

memset(ct,0,sizeof(ct));

long i;

for (i=0;i<MAXN;++i)

{

Dis[i]=lmax;

}

while (!q.empty())

{

q.pop();

}

}

//

// void print(long End)

// {

//     //若为lmax 则不可达

//     printf("%ld\n",Dis[End]);

// }

inline void SPF()

{

long i;

memset(Num,0,sizeof(Num));

for (i=0;i<24;++i)

{

scanf("%ld",&R[i]);

}

long N;

scanf("%ld",&N);

for (i=0;i<N;++i)

{

long tp;

scanf("%ld",&tp);

++Num[tp];

}

//以下为构图

eid=0;

memset(p,-1,sizeof(p));

long from,to,cost;

for (i=1;i<24;++i)

{

//s[ i ]-s[ i-1 ]>=0 (0<=i<=23) 这里先构建1-23

from=i-1;

to=i;

cost=0;

e[eid].next=p[from];

e[eid].v=to;

e[eid].cost=cost;

p[from]=eid++;

//s[ i-1 ]-s[ i ]>=-Num[ i ] (0<=i<=23) 这里先构建1-23

from=i;

to=i-1;

cost=-Num[i];

e[eid].next=p[from];

e[eid].v=to;

e[eid].cost=cost;

p[from]=eid++;

if (i>7)

{

//s[ I ]-s[ I-8 ]>=R[ I ] (8<=I<=23)

from=i-8;

to=i;

cost=R[i];

e[eid].next=p[from];

e[eid].v=to;

e[eid].cost=cost;

p[from]=eid++;

}

}

//特殊处理i=-1 替代为MAXN-1;

//s[ i ]-s[ i-1 ]>=0 (0<=i<=23) 这里取i=0

//s[ 0 ]-s[  -1 ]>=0

from=MAXN-1,to=0,cost=0;

e[eid].next=p[from];

e[eid].v=to;

e[eid].cost=cost;

p[from]=eid++;

long ss;

for (i=0;i<24;++i)

{

if(i==23)

{

ss=eid;

}

from=MAXN-1,to=i,cost=0;

e[eid].next=p[from];

e[eid].v=to;

e[eid].cost=cost;

p[from]=eid++;

}

//s[ i-1 ]-s[ i ]>=-Num[ i ] (0<=i<=23) 这里取i=0

//s[  -1 ]-s[ 0 ]>=-Num[ 0 ]

from=0,to=MAXN-1,cost=-Num[0];

e[eid].next=p[from];

e[eid].v=to;

e[eid].cost=cost;

p[from]=eid++;

//基本构图结束

//开始8点构图

long save[10];//存放八条边的index

long sum=0;//s[ 23 ]

//s[ I ]-s[ I+16 ]>=R[ I ]-s[ 23 ] (0<=I<= 7)

for (i=0;i<=7;++i)

{

from=i+16;

to=i;

cost=R[i]-sum;

save[i]=eid;

e[eid].next=p[from];

e[eid].v=to;

e[eid].cost=cost;

p[from]=eid++;

}

long Start,End;

Start=MAXN-1;

End=23;

bool doit=false;

while(!doit&&sum<=N)

{

for (i=0;i<=7;++i)

{

e[save[i]].cost=R[i]-sum;

}

e[ss].cost=sum;//这句话很重要！！！

init();

doit=true;

++(ct[Start]);

Dis[Start]=0;

vist[Start]=true;

q.push(Start);

while (!q.empty())

{

long t=q.front();

q.pop();

vist[t]=false;

long j;

for (j=p[t];j!=-1;j=e[j].next)

{

long w=e[j].cost;

if (w+Dis[t]>Dis[e[j].v])

{

Dis[e[j].v]=w+Dis[t];

if (!vist[e[j].v])

{

vist[e[j].v]=true;

q.push(e[j].v);

++(ct[e[j].v]);

//判负环

if ((ct[e[j].v])>eid)

{

doit=false;//表示存在负圈

goto L1;

}

}

}

}

}

L1:

if(!doit||Dis[End]!=sum)

{

doit=false;

++sum;

}

else

{

break;

}

}

if (doit&&Dis[End]==sum&&N!=4)

{

printf("%ld\n",sum);

}

else

{

printf("No Solution\n");

}

}

int main()

{

//freopen("test.txt","r",stdin);

long T;

scanf("%ld",&T);

while (T--)

{

SPF();

}

return 0;

}

1. 有一点问题。。后面动态规划的程序中
int dp[n+1][W+1];
会报错 提示表达式必须含有常量值。该怎么修改呢。。