poj 1275 Cashier Employment 差分约束

差分约束模板题,差分约束是判断联立不等式组是否有解的一种方法,建图是关键。

代码:

//poj 1275
//sep9
#include <iostream>
#include <queue>
using namespace std;
const int maxM=10024;
const int maxN=32;

struct Edge
{
	int v,w,nxt;
}edge[maxM];
int t[maxN],c[maxN],head[maxN],vis[maxN],inq[maxN],dis[maxN];
int e;

void addedge(int u,int v,int w)
{
	edge[e].v=v;edge[e].w=w;edge[e].nxt=head[u];
	head[u]=e++;
}

bool spfa()
{
	memset(inq,0,sizeof(inq));
	for(int i=0;i<=24;++i) dis[i]=INT_MIN;
	memset(vis,0,sizeof(vis));
	queue<int> Q;
	Q.push(0);
	dis[0]=0;
	inq[0]=1;
	vis[0]=1;
	while(!Q.empty()){
		int u=Q.front();Q.pop();inq[u]=0;
		for(int i=head[u];i!=-1;i=edge[i].nxt){
			int v=edge[i].v,w=edge[i].w;
			if(dis[v]<dis[u]+w){
				dis[v]=dis[u]+w;
				if(inq[v]==0){
					Q.push(v);
					++vis[v];
					inq[v]=1;
					if(vis[v]>=25)
						return false;
				}
			}
		}
	}
	return true;
}

int main()
{
	int cases;
	scanf("%d",&cases);
	while(cases--){
		memset(head,-1,sizeof(head));
		memset(t,0,sizeof(t));
		for(int i=1;i<=24;++i)
			scanf("%d",&c[i]);
		int n;
		scanf("%d",&n);
		for(int i=0;i<n;++i){
			int x;
			scanf("%d",&x);
			++t[x+1];
		}
		int l=0,r=n+1,ans=-1,mid;
		while(l<r){
			mid=(l+r)/2;
			memset(head,-1,sizeof(head));
			e=0;
			for(int i=0;i<24;++i){
				addedge(i,i+1,0);
				addedge(i+1,i,-t[i+1]);
			}
			addedge(0,24,mid);
			for(int j=1;j<=24;++j){
				int i=j+8;
				if(i<=24)
					addedge(j,i,c[i]);
				else{
					i=j+8-24;
					addedge(j,i,c[i]-mid);
				}
			}
			if(spfa()){
				ans=mid;
				r=mid;
			}else{
				l=mid+1;
			}
		}
		if(ans!=-1)
			printf("%d\n",ans);
		else
			puts("No Solution");
	}
	return 0;
}
时间: 2024-11-10 00:55:33

poj 1275 Cashier Employment 差分约束的相关文章

poj 1275 Cashier Employment - 差分约束 - 二分答案

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 d

poj——1275 Cashier Employment 差分约束系统

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 di

图论(差分约束系统):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 hir

POJ1275/ZOJ1420/HDU1529 Cashier Employment (差分约束)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 题意:一商店二十四小时营业,但每个时间段需求的出纳员不同,现有n个人申请这份工作,其可以从固定时间t连续工作八个小时,问在满足需求的情况下最小需要多少个出纳 一道十分经典的差分约束题目,在构图上稍有难度 为避免负数,时间计数1-24. 令: r[i]表示i时间需要的人数 (1<=i<=24) num[i]表示i时间应聘的人数 (1<=i<=24) x[i]表示i时间

HDU.1529.Cashier Employment(差分约束 最长路SPFA)

题目链接 \(Description\) 给定一天24h 每小时需要的员工数量Ri,有n个员工,已知每个员工开始工作的时间ti(ti∈[0,23]),每个员工会连续工作8h. 问能否满足一天的需求.若能,输出最少需要多少员工. \(Solution\) 参考. 既然给的是开始工作时间,那么就先根据开始时间做 设Ai表示在i时开始工作的人数(未知),Bi表示i时可工作人数的上限(已知) 那么有:(注意可以跨天) A[i-7]+A[i-6]+...+A[i-1]+A[i] >= R[i] (7 <

Cashier Employment 差分约束

题意:有一个超市需要一些出纳员,已给出这个超市在各个时间段(0-1,1-2,2-3...共24个时间段)至少需要的出纳员数目,现在前来应聘有n个人,每个人都有一个固定的开始工作的时间,这也意味着从这个时间开始他要连续工作8个小时.在满足这个超市对出纳员的需求的前提下,让你求出最少雇佣的出纳员人数. need[i]表示在第 i 个小时至少也要的人数,work[i]表示应聘者中可以在第i个小时开始工作的人数,s[i]表示前i个小时雇佣的人数, x[ i ]表示第 i 个小时雇佣的人数. s[i] -

poj 3169 Layout(差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 3168 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

POJ1275 Cashier Employment[差分约束系统 || 单纯形法]

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

POJ 3159 Candies(差分约束+spfa+链式前向星)

题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A的糖果数<=C . 最后求n 比 1 最多多多少颗糖果. 解题思路:经典差分约束的题目,具体证明看这里<数与图的完美结合——浅析差分约束系统>. 不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值,我们得到 dis[B]-dis[A]<=w(A,B).看到这里,我们可以联想到