POJ 1275 差分约束系统+二分

题目

Cashier Employment
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7692 Accepted: 2906
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

题意

有一个商店要找一些服务员。这个商店在24小时内每个小时需要的人手是已知的。有一些人来应聘,每个人从一个特定的时刻往后可以工作八小时。问最少要招多少个人可以满足商店的要求。

题解

此题在黑书《算法艺术和信息学竞赛》里有。
s[i]为[0,i]区间里有s[i]个人
sum为拟雇佣的人数
约束条件为:
s[i] - s[i-1]>=0,s[i]肯定不比s[i-1]小
s[i-1]+ t[i]>=s[i],加上可以雇佣的人肯定不小于s[i]
再考虑必须满足每个时刻的需要值
s[ i ]-s[ i-8 ]>=r[ i ] (8<=I<=23)
s[ i ]-s[ i+16 ]>=r[ i ]-s[ 23 ] (0<=I<= 7)
然后是总的约束条件
s[23]-s[-1]>=sum
然后外层二分求sum,内层用Bellman-ford维护约束条件即可。

//
//  main.cpp
//  poj1275
//
//  Created by MuChen on 16/4/28.
//  Copyright © 2016年 MuChen. All rights reserved.
//

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
#include <iomanip>
#define m0(a) memset(a,0,sizeof(a))
#define mm(a) memset(a,0x3f,sizeof(a))
#define m_1(a) memset(a,-1,sizeof(a))
#define f(i,a,b) for(i = (a);i<=(b);i++)
#define fi(i,a,b) for(i = a;i>=b;i--)
#define lowbit(a) ((a)&(-a))
#define FFR freopen("data.in","r",stdin)
#define FFW freopen("data.out","w",stdout)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef long double ld;
const ld PI = acos(-1.0);

using namespace std;
#define SIZE ( )

template<typename T> inline void read(T &x) {
    x = 0; T f = 1; char ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
    while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
    x *= f;
}

struct Edge {
    int x, y, v;
};

Edge G[1000];

int r[30];
int t[30];
int s[30];
int n;
int ans;
int cnt;

inline void Push_back(int u,int v,int w){
    G[cnt].x = u;G[cnt].y = v;G[cnt].v = w;
    cnt++;
}

int Bellman(int sum){
    int i,j;
    mm(s);
    s[0] = 0;
    f(i,0,24){
        int ok = 0;
        f(j,0,cnt - 1){
            if(s[G[j].x]>s[G[j].y]+G[j].v){
                ok = 1;
                s[G[j].x] = s[G[j].y]+G[j].v;
            }
        }
        if(!ok) break;
    }
    f(i,0,cnt-1)
        if(s[G[i].x]>s[G[i].y]+G[i].v)
            return 0;
    return 1;
}

int main() {
    //ios_base::sync_with_stdio(false); cin.tie(0);
    int N;
    read(N);
    while (N--) {
        int i;
        f(i,1,24) read(r[i]);
            read(n);
        m0(t);
        f(i,1,n){
            int x;
            read(x);
            t[x+1]++;
        }
        cnt = 0;
        f(i,1,24){
            Push_back(i-1,i,t[i]);
            Push_back(i,i-1,0);
        }

        ans = -1;

        int Left = 0,Right = n;
        while (Left<=Right) {
            int m = (Left+Right)>>1;
            cnt = 48;
            f(i,1,16) Push_back(i+8,i,-r[i+8]);
            f(i,17,24) Push_back(i-16,i,m-r[i-16]);
            Push_back(24, 0, -m);

            if(Bellman(m)){
                ans = m;
                Right = m - 1;
            }
            else
                Left = m + 1;
        }
        if(-1==ans)
            printf("No Solution\n");
        else
            printf("%d\n",ans);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值