poj 1275 Cashier Employment

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

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

Tehran 2000

分析:
分析:
我们按刚才所讲到的方法对此题进行处理。这题很容易想到如下的不等式模型:
设num[ i ]为i时刻能够开始工作的人数,x[ 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)
这里出现了小的困难,我们发现以上式子并不是标准的差分约束系统,因为在最后一个式子中出现了三个未知单位。但是注意到其中跟随 I变化的只有两个,于是s[23]就变得特殊起来,看来是需要我们单独处理,于是我们把 s[ 23 ]当作已知量放在右边。
经过这样的整理,整个图就很容易创建了,将所有形如 A-B>=C 的式子 我们从节点B 引出一条有向边指向 A 边的权值为C (这里注意由于左右确定,式子又是统一的>=的不等式,所以A和B是相对确定的,边是一定是指向A的) ,图就建成了 。
最后枚举所有s[ 23 ]的可能值,对于每一个s[23],我们都进行一次常规差分约束系统问题的求解,判断这种情况是否可行,如果可行求出需要的最优值,记录到Ans中,最后的Ans的值即为所求。

代码:

Source Code

Problem: 1275       
Memory: 892K        Time: 63MS
Language: Pascal        Result: Accepted
Source Code
const
  maxn=1000;
type
  node=record
    u,v,w:longint;
  end;
var
  n,m,casen,casei:longint;
  e:array[1..maxn] of node;
  t,r,d:array[-1..maxn] of longint;

procedure add(x,y,z:longint);
  begin
    inc(m);
    with e[m] do
    begin
      u:=x; v:=y; w:=z;
    end;
  end;

procedure init;
  var
    i,x:longint;
  begin
    for i:=0 to 23 do read(r[i]);
    readln;
    readln(n);
    fillchar(t,sizeof(t),0);
    for i:=1 to n do
      begin readln(x); inc(t[x]); end;
  end;

procedure main;
  var
    i,j,min:longint;
    check:boolean;
  begin
    for min:=1 to n do
    begin
      m:=0;
      for i:=-1 to 23 do
        d[i]:=100000;
      add(23,-1,-min);
      for i:=0 to 23 do
        begin
          add(i,i-1,0); add(i-1,i,t[i]);
          if i>7 then add(i,i-8,-r[i]) else add(i,i+16,-r[i]+min);
        end;
      for j:=0 to 23 do
        begin
          check:=true;
          for i:=1 to m do
            with e[i] do
            if d[v]>d[u]+w then
              begin
                d[v]:=d[u]+w;
                check:=false;
              end;
          if check then break;
        end;
      check:=true;
      for i:=1 to m do
        with e[i] do
        if d[v]>d[u]+w then begin check:=false; break; end;
      if check then begin writeln(min); exit; end;
    end;
    writeln('No Solution');
  end;

begin
  readln(casen);
  for casei:=1 to casen do
    begin
      init;
      main;
    end;
end.
const
  maxn=1000;
type
  node=record
    u,v,w:longint;
  end;
var
  n,m,casen,casei:longint;
  e:array[1..maxn] of node;
  t,r,d:array[-1..maxn] of longint;

procedure add(x,y,z:longint);
  begin
    inc(m);
    with e[m] do
    begin
      u:=x; v:=y; w:=z;
    end;
  end;

procedure init;
  var
    i,x:longint;
  begin
    for i:=0 to 23 do read(r[i]);
    readln;
    readln(n);
    fillchar(t,sizeof(t),0);
    for i:=1 to n do
      begin readln(x); inc(t[x]); end;
  end;

procedure main;
  var
    i,j,min:longint;
    check:boolean;
  begin
    for min:=1 to n do
    begin
      m:=0;
      for i:=-1 to 23 do
        d[i]:=100000;
      add(23,-1,-min);
      for i:=0 to 23 do
        begin
          add(i,i-1,0); add(i-1,i,t[i]);
          if i>7 then add(i,i-8,-r[i]) else add(i,i+16,-r[i]+min);
        end;
      for j:=0 to 23 do
        begin
          check:=true;
          for i:=1 to m do
            with e[i] do
            if d[v]>d[u]+w then
              begin
                d[v]:=d[u]+w;
                check:=false;
              end;
          if check then break;
        end;
      check:=true;
      for i:=1 to m do
        with e[i] do
        if d[v]>d[u]+w then begin check:=false; break; end;
      if check then begin writeln(min); exit; end;
    end;
    writeln('No Solution');
  end;

begin
  readln(casen);
  for casei:=1 to casen do
    begin
      init;
      main;
    end;
end.
const
  maxn=1000;
type
  node=record
    u,v,w:longint;
  end;
var
  n,m,casen,casei:longint;
  e:array[1..maxn] of node;
  t,r,d:array[-1..maxn] of longint;

procedure add(x,y,z:longint);
  begin
    inc(m);
    with e[m] do
    begin
      u:=x; v:=y; w:=z;
    end;
  end;

procedure init;
  var
    i,x:longint;
  begin
    for i:=0 to 23 do read(r[i]);
    readln;
    readln(n);
    fillchar(t,sizeof(t),0);
    for i:=1 to n do
      begin readln(x); inc(t[x]); end;
  end;

procedure main;
  var
    i,j,min:longint;
    check:boolean;
  begin
    for min:=1 to n do
    begin
      m:=0;
      for i:=-1 to 23 do
        d[i]:=100000;
      add(23,-1,-min);
      for i:=0 to 23 do
        begin
          add(i,i-1,0); add(i-1,i,t[i]);
          if i>7 then add(i,i-8,-r[i]) else add(i,i+16,-r[i]+min);
        end;
      for j:=0 to 23 do
        begin
          check:=true;
          for i:=1 to m do
            with e[i] do
            if d[v]>d[u]+w then
              begin
                d[v]:=d[u]+w;
                check:=false;
              end;
          if check then break;
        end;
      check:=true;
      for i:=1 to m do
        with e[i] do
        if d[v]>d[u]+w then begin check:=false; break; end;
      if check then begin writeln(min); exit; end;
    end;
    writeln('No Solution');
  end;

begin
  readln(casen);
  for casei:=1 to casen do
    begin
      init;
      main;
    end;
end.
const
  maxn=1000;
type
  node=record
    u,v,w:longint;
  end;
var
  n,m,casen,casei:longint;
  e:array[1..maxn] of node;
  t,r,d:array[-1..maxn] of longint;

procedure add(x,y,z:longint);
  begin
    inc(m);
    with e[m] do
    begin
      u:=x; v:=y; w:=z;
    end;
  end;

procedure init;
  var
    i,x:longint;
  begin
    for i:=0 to 23 do read(r[i]);
    readln;
    readln(n);
    fillchar(t,sizeof(t),0);
    for i:=1 to n do
      begin readln(x); inc(t[x]); end;
  end;

procedure main;
  var
    i,j,min:longint;
    check:boolean;
  begin
    for min:=1 to n do
    begin
      m:=0;
      for i:=-1 to 23 do
        d[i]:=100000;
      add(23,-1,-min);
      for i:=0 to 23 do
        begin
          add(i,i-1,0); add(i-1,i,t[i]);
          if i>7 then add(i,i-8,-r[i]) else add(i,i+16,-r[i]+min);
        end;
      for j:=0 to 23 do
        begin
          check:=true;
          for i:=1 to m do
            with e[i] do
            if d[v]>d[u]+w then
              begin
                d[v]:=d[u]+w;
                check:=false;
              end;
          if check then break;
        end;
      check:=true;
      for i:=1 to m do
        with e[i] do
        if d[v]>d[u]+w then begin check:=false; break; end;
      if check then begin writeln(min); exit; end;
    end;
    writeln('No Solution');
  end;

begin
  readln(casen);
  for casei:=1 to casen do
    begin
      init;
      main;
    end;
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值