• poj1275 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

    差分约束,推得很麻烦。
    推荐一个题解(其实也不算题解,是一个我觉得讲的很好的差分约束,正好有这个例题。)
    http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html
      1 #include <iostream>
      2 #include <cstdlib>
      3 #include <cstring>
      4 #include <cstdio>
      5 #include <queue>
      6 const int N = 1000 + 11,inf = 1 << 30;
      7 using namespace std;
      8 int T,n,ned[25],s[25],hav[25],tot,head[25],dis[27],cs[27];
      9 bool vis[25];
     10 struct id
     11 {
     12     int to,nxt,w; int qs;
     13 }E[25*30];
     14 
     15 void Clear()
     16 {
     17     memset(hav,0,sizeof(hav)); tot = 0;
     18     memset(head,-1,sizeof(head));
     19 }
     20 
     21 void add(int u,int v,int w,int cy)
     22 {
     23 
     24     E[++tot] = (id){v,head[u],w,cy}; head[u] = tot;
     25 }
     26 
     27 void build()
     28 {
     29 
     30     add(24,0,0,-1); add(0,24,0,1);
     31     int i;
     32     for(i = 1; i <= 24; ++i) add(i,i-1,0,0),add(i-1,i,hav[i],0);
     33     for(i = 8; i <= 24; ++i) add(i,i-8,-ned[i],0);
     34     for(i = 1; i < 8; ++i) add(i,i+16,ned[i],1);
     35 }
     36 
     37 void Init()
     38 {
     39     Clear();
     40     int i,j;
     41     for(i = 1; i <= 24; ++i) scanf("%d",ned+i);
     42     scanf("%d",&n);
     43     for(i = 1; i <= n; ++i) { scanf("%d",&j); ++hav[j+1]; }
     44 }
     45 
     46 bool cheak(int sum)
     47 {
     48 
     49     memset(vis,0,sizeof(vis)); 
     50     memset(cs,0,sizeof(cs));
     51     memset(dis,127/2,sizeof(dis));
     52     queue<int>Q;
     53     while(!Q.empty()) Q.pop();
     54     Q.push(0); dis[0] = 0; cs[0] = vis[0] = true;
     55     while(!Q.empty())
     56     {
     57         int u = Q.front(),v,i,w; Q.pop();
     58         if(cs[u] > 24) return false; 
     59         ++cs[u], vis[u] = false;
     60         
     61         for(i = head[u]; ~i; i = E[i].nxt)
     62         {
     63             v = E[i].to;     
     64             if(E[i].qs == -1) w = -sum;else w =(E[i].qs==1)?sum - E[i].w:E[i].w;
     65             if(dis[v] > dis[u] + w)
     66             {
     67                 dis[v] = dis[u] + w;
     68                 if(!vis[v]) { Q.push(v); vis[v] = true;}
     69             }
     70         }
     71     }
     72     if(dis[24] == dis[25]) return false;
     73     return true;
     74 }
     75 
     76 
     77 void Binary_search()
     78 {
     79     int l = 0,r = n,mid,ret = n+1;
     80     while(l <= r)
     81     {
     82         mid = (l + ((r-l)>>1)) ;
     83         if(cheak(mid)) ret = mid, r = mid - 1;
     84         else l = mid + 1;
     85     }
     86     if(ret == n + 1) puts("No Solution");
     87     else printf("%d
    ",ret);
     88 }
     89 
     90 int main()
     91 {
     92     scanf("%d",&T);
     93     while(T--)
     94     {
     95         Init();
     96         build();
     97         Binary_search();
     98     }
     99     return 0;
    100 }
  • 相关阅读:
    如何将数据库中已有表导入到powerDesigner生成pdm文件
    Delphi TcxTreelist 表格左边总是缩进去 ,好像有偏移 解决方法
    HTML、CSS、JS对unicode字符的不同处理
    老生常谈ajax
    浅谈javascript面向对象
    HTML5原生拖放实例分析
    一个小动画,颠覆你的CSS世界观
    布局神器display:table-cell
    javascript 日常总结
    G2 2.0 更灵活、更强大、更完备的可视化引擎!
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6612733.html
Copyright © 2020-2023  润新知