您的位置:首页 > 其它

zoj 1420 or poj 1275 差分约束

2013-08-25 01:02 232 查看
Cashier EmploymentTime 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代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<queue>
#include<algorithm>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define MAX_LL 0x7fffffffffffffff
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))

#define MAXN 30
#define MAXM 111
#define INF 100000

struct edge{
int u,v,w,nxt;
}e[MAXM],ep[MAXM];

int cc,h[MAXN],hp[MAXN];
int r[MAXN],num[MAXN];

int inq[MAXN],cnt[MAXN],d[MAXN];
queue<int> q;

int bford(int s, int n){
memset(cnt, 0, sizeof(cnt));
memset(inq, 0, sizeof(inq));
while(!q.empty()) q.pop();

for(int i=0; i<n; i++) d[i]=INF;    d[s]=0;
q.push(s);  inq[s]=1;
while(!q.empty()){
int u=q.front();    q.pop();    inq[u]=0;
for(int i=h[u]; i!=-1; i=e[i].nxt){
int v=e[i].v, w=e[i].w;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!inq[v]){
if(++cnt[v]>n) return 1;
q.push(v);
inq[v]=1;
}
}
}
}
return 0;
}

inline void add(int u, int v, int w, int f){
if(f){
e[cc]=(edge){u,v,w,h[u]};
h[u]=cc++;
}
else{
ep[cc]=(edge){u,v,w,hp[u]};
hp[u]=cc++;
}
}

inline void ini(){
cc=0;
memset(hp, -1, sizeof(hp));
}

int main(){
//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
int T;
scanf(" %d",&T);
while(T--){
int i,j;
for(i=1; i<25; i++){
scanf(" %d",r+i);
}
int n;
scanf(" %d",&n);
memset(num, 0, sizeof(num));
for(i=0; i<n; i++){
scanf(" %d",&j);    num[j+1]++;
}

ini();
for(i=1; i<25; i++){
add(i, i-1, 0, 0);                     // S[i-1] - S[i] <= 0
add(i-1, i, num[i], 0);                // S[i] - S[i-1] <= num[i]
}
for(i=9; i<25; i++) add(i, i-8, -r[i], 0); // S[i-8] - S[i] <= -r[i]
//for(i=1; i<25; i++) add(0, i, 0, 0);

int tc=cc;
for(j=0; j<=n; j++){
cc=tc;                                 //重新建图
memcpy(h, hp, sizeof(hp));
memcpy(e, ep, sizeof(e));
for(i=1; i<9; i++)
add(i, i+16, j-r[i], 1);           // S[16+i] - S[i] <= S[24] - r[i]
add(24, 0, -j, 1);                     //
if(!bford(24, 25)) break;
}
if(j<=n) printf("%d\n",j);
else printf("No Solution\n");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: