zoukankan      html  css  js  c++  java
  • zoj 1420 or poj 1275 差分约束

    Cashier Employment

    Time Limit: 10 Seconds      Memory Limit: 32768 KB

    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

    这里有几个给力的题解:

    论文:http://pan.baidu.com/share/link?shareid=508660&uk=2720516383&fid=743287535

    http://blog.csdn.net/popopopolo/article/details/6670425?reload

    http://blog.himdd.com/archives/1060

    http://blog.csdn.net/chinaczy/article/details/5765553

    自己理理思路

    s[i]截止i时刻都工作的人数;r[i],i至i+1应该工作的人数; num[i],从i开始工作的人数

    s[i] - s[i-8] >= r[i]           (9<i<=24)

    s[i] + s[24] -s[16+i] >= r[i]     (1<=i<=8)       这里可以自己推,想像一个圆,固定长度个连续元素和

    0 <= s[i] - s[i-1] <= num[i]    (1<= i <= 24)

    其实还有一个不太清楚的:S[0] <= S[24] - ans

    之后就建图,用0作源点WA了,可能和不理解上面的不等式有关。。。AC代码:

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<string>
      5 #include<cmath>
      6 #include<vector>
      7 #include<cstdlib>
      8 #include<queue>
      9 #include<algorithm>
     10 
     11 using namespace std;
     12 
     13 #define LL long long
     14 #define ULL unsigned long long
     15 #define UINT unsigned int
     16 #define MAX_INT 0x7fffffff
     17 #define MAX_LL 0x7fffffffffffffff
     18 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
     19 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
     20 
     21 #define MAXN 30
     22 #define MAXM 111
     23 #define INF 100000
     24 
     25 struct edge{
     26     int u,v,w,nxt;
     27 }e[MAXM],ep[MAXM];
     28 
     29 int cc,h[MAXN],hp[MAXN];
     30 int r[MAXN],num[MAXN];
     31 
     32 int inq[MAXN],cnt[MAXN],d[MAXN];
     33 queue<int> q;
     34 
     35 int bford(int s, int n){
     36     memset(cnt, 0, sizeof(cnt));
     37     memset(inq, 0, sizeof(inq));
     38     while(!q.empty()) q.pop();
     39 
     40     for(int i=0; i<n; i++) d[i]=INF;    d[s]=0;
     41     q.push(s);  inq[s]=1;
     42     while(!q.empty()){
     43         int u=q.front();    q.pop();    inq[u]=0;
     44         for(int i=h[u]; i!=-1; i=e[i].nxt){
     45             int v=e[i].v, w=e[i].w;
     46             if(d[v]>d[u]+w){
     47                 d[v]=d[u]+w;
     48                 if(!inq[v]){
     49                     if(++cnt[v]>n) return 1;
     50                     q.push(v);
     51                     inq[v]=1;
     52                 }
     53             }
     54         }
     55     }
     56     return 0;
     57 }
     58 
     59 inline void add(int u, int v, int w, int f){
     60     if(f){
     61         e[cc]=(edge){u,v,w,h[u]};
     62         h[u]=cc++;
     63     }
     64     else{
     65         ep[cc]=(edge){u,v,w,hp[u]};
     66         hp[u]=cc++;
     67     }
     68 }
     69 
     70 inline void ini(){
     71     cc=0;
     72     memset(hp, -1, sizeof(hp));
     73 }
     74 
     75 int main(){
     76     //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
     77     int T;
     78     scanf(" %d",&T);
     79     while(T--){
     80         int i,j;
     81         for(i=1; i<25; i++){
     82             scanf(" %d",r+i);
     83         }
     84         int n;
     85         scanf(" %d",&n);
     86         memset(num, 0, sizeof(num));
     87         for(i=0; i<n; i++){
     88             scanf(" %d",&j);    num[j+1]++;
     89         }
     90 
     91         ini();
     92         for(i=1; i<25; i++){
     93             add(i, i-1, 0, 0);                     // S[i-1] - S[i] <= 0
     94             add(i-1, i, num[i], 0);                // S[i] - S[i-1] <= num[i]
     95         }
     96         for(i=9; i<25; i++) add(i, i-8, -r[i], 0); // S[i-8] - S[i] <= -r[i]
     97         //for(i=1; i<25; i++) add(0, i, 0, 0);
     98 
     99         int tc=cc;
    100         for(j=0; j<=n; j++){
    101             cc=tc;                                 //重新建图
    102             memcpy(h, hp, sizeof(hp));
    103             memcpy(e, ep, sizeof(e));
    104             for(i=1; i<9; i++)
    105                 add(i, i+16, j-r[i], 1);           // S[16+i] - S[i] <= S[24] - r[i]
    106             add(24, 0, -j, 1);                     // 
    107             if(!bford(24, 25)) break;
    108         }
    109         if(j<=n) printf("%d
    ",j);
    110         else printf("No Solution
    ");
    111     }
    112     return 0;
    113 }
    View Code

  • 相关阅读:
    Windows 10+Ubuntu 16.04在MBR分区上安装双系统(转)
    安装Ubuntu 16.04时出现:没有定义根文件系统,请到分区菜单修改
    Windows 10+Ubuntu 16.04在MBR分区上安装双系统之后启动菜单的System Setup选项提示:can't find command "fwsetup"
    Ubuntu 16.04安装GTX960闭源驱动
    Windows 10+Ubuntu 16.04在MBR分区上安装双系统之后没有Windows 10的启动菜单解决方法
    Ubuntu16.04安装deb文件时提示:此软件来自第三方且可能包含非自由组件
    完全卸载SQL Server 2008 R2(转)
    Windows超级卸载工具Total Uninstaller,能完全卸载.NET Framework
    Windows 10卸载Edge浏览器(不成功的别试了)
    完全卸载VS2015的方法
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3280246.html
Copyright © 2011-2022 走看看