zoukankan      html  css  js  c++  java
  • 图论(差分约束系统):POJ 1275 Cashier Employment

    Cashier Employment
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 7651   Accepted: 2886

    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

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #define INF -1000000000
     5 using namespace std;
     6 int r[25],t[25];
     7 int cnt,fir[2010],nxt[2010],to[2010],val[2010];
     8 void addedge(int a,int b,int v)
     9 {
    10     nxt[++cnt]=fir[a];
    11     fir[a]=cnt;to[cnt]=b;
    12     val[cnt]=v;
    13 }
    14 int n,ans;
    15 int dis[30],in[30],vis[30],q[50010];
    16 bool Spfa()
    17 {
    18     int S=0,f=1,b=1;
    19     for(int i=0;i<=24;i++)
    20         dis[i]=INF;
    21     memset(in,0,sizeof(in));
    22     memset(vis,0,sizeof(vis));
    23     
    24     q[b++]=S;
    25     dis[S]=0;
    26     in[S]++;
    27     vis[S]=1;
    28     while(f<b){
    29         int node=q[f++];vis[node]=false;
    30         for(int i=fir[node];i;i=nxt[i])
    31             if(dis[to[i]]<dis[node]+val[i]){
    32                 dis[to[i]]=dis[node]+val[i];
    33                 if(!vis[to[i]]){
    34                     if(++in[to[i]]>n)
    35                         return false;
    36                     q[b++]=to[i];
    37                     vis[to[i]]=true;
    38                 }
    39             }
    40     }
    41     return dis[24]==ans;
    42 }
    43 
    44 void Build()
    45 {
    46     memset(fir,0,sizeof(fir));cnt=0;
    47     for(int i=8;i<=24;i++)
    48         addedge(i-8,i,r[i]);
    49         
    50     for(int i=1;i<=7;i++)
    51         addedge(i+16,i,r[i]-ans);
    52     for(int i=0;i<24;i++){
    53         addedge(i+1,i,-t[i]);
    54         addedge(i,i+1,0);
    55     }
    56     addedge(0,24,ans);    
    57 }
    58 
    59 void Solve()
    60 {
    61     int r=0,h=n+1;
    62     while(h-r>1)
    63     {
    64         ans=(r+h)>>1;
    65         Build();
    66         if(Spfa())
    67             h=ans;
    68         else
    69             r=ans;
    70     }
    71     if(h==n+1)
    72         printf("No Solution
    ");
    73     else
    74         printf("%d
    ",h);
    75 }
    76 
    77 int main(){
    78     int T;int x;
    79     scanf("%d",&T);
    80     while(T--)
    81     {
    82         for(int i=1;i<=24;i++)
    83             scanf("%d",&r[i]);
    84         scanf("%d",&n);
    85         memset(t,0,sizeof(t));
    86         for(int i=1;i<=n;i++){
    87             
    88             scanf("%d",&x);
    89             ++t[x];
    90         }
    91         Solve();
    92     }
    93     return 0;
    94 }    

      
    尽最大的努力,做最好的自己!
  • 相关阅读:
    JavaScript的执行
    关于k阶裴波那契序列的两种解法
    科普 eclipse中的Java build
    [BZOJ 1037] 生日聚会Party
    [POJ 1185] 炮兵阵地
    [POJ 1935] Journey
    [POJ 2397] Spiderman
    [POJ 2373][BZOJ 1986] Dividing the Path
    [POJ 3378] Crazy Thairs
    [POJ 2329] Nearest number-2
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5272103.html
Copyright © 2011-2022 走看看