【差分约束】zoj两题

差分约束系统,出纳员的雇佣 Tehran的一家每天24小时营业的超市,需要一批出纳员来满足它的需要。超市经理雇佣你来帮他解决问题:超市在每天的不同时段需要不同数目的出纳员(例如:午夜时只需一小批,而下午则需要很多)来为顾客提供优质服务。他希望雇佣最少数目的出纳员。 经理已经提供你一天的每一小时需要出纳员的最少数量——R(0), R(1), …, R(23)。R(0)表示从午夜到上午1:00需要出纳员的最少数目,R(1)表示上 阅读详情

【我是为了写总结...求轻虐...】

zoj1420(出纳员雇佣)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1420

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<climits>
#include<cctype>
#define maxe 20005
#define maxq 1000

using namespace std;

int n, r[25], p[25], que[maxq], tim[25], dis[25], head, tail, ans;
bool iq[25];

int t[maxe], c[maxe], nt[maxe], h[25], tot;

void adde(int a, int b, int x)
{
	t[tot] = b; c[tot] = x; 
	nt[tot] = h[a]; h[a] = tot++;
}

bool relax(int a, int b, int x)
{
	if (dis[a] + x > dis[b])
	{
		dis[b] = dis[a] + x; if (!iq[b]) return 1;
	}
	return 0;
}

bool spfa(int ans)
{
	int i;
	tot = 1;
	memset(h, 0, sizeof(h));
	memset(iq, 0, sizeof(iq));
	memset(tim, 0, sizeof(tim));
	for (i = 1; i <= 24; i++) dis[i] = -1000000000;
	for (i = 1; i <= 24; i++) 
		adde(i, i - 1, -p[i]) ,adde(i - 1, i, 0);
	for (i = 1; i <= 8; i++) 
		adde(i + 16, i, r[i] - ans);
	for (i = 9; i <= 24; i++) 
		adde(i - 8, i, r[i]);
	adde(0, 24, ans);
	head = 1, tail = 1;
	dis[0] = 0; que[1] = 0; iq[0] = 1; tim[0] = 1;
	int p, qt, q;
	while (head <= tail)
	{
		p = que[head];
		iq[p] = 0;
		for (q = h[p]; q; q = nt[q])
			if (relax(p, t[q], c[q]))
			{
				qt = t[q]; iq[qt] = 1;
				if (++tim[qt] > 24) return 0;
				if (++tail > maxq) tail = 1;
				que[tail] = qt;
			}
		if (++head > maxq) head = 1;
	}
	if (dis[24] == ans) return 1;
		else return 0;
}

bool getans()
{
	for(ans = 0; ans <= n; ans++) if (spfa(ans)) return 1;
	return 0;
}

int main()
{
	freopen("test.in", "r", stdin);
	freopen("test.out", "w", stdout);
	int stime, i, ad;
	scanf("%d", &stime);
	while(stime--)
	{
		for (i = 1; i <= 24; i++) scanf("%d", &r[i]);
		scanf("%d", &n);
		for (i = 1; i <= n; i++) 
		{
			scanf("%d", &ad);
	   	++p[ad + 1];
		}
		if (getans()) printf("%d\n", ans); else puts("No Solution");
	}
	return 0;
}

zoj2770 火烧连营

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2770

code:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<cctype>
#define maxn 10100
#define maxm 10010
#define maxe 50010
using namespace std;

int i, j, k, n, m, c, tot = 0, h[maxn], nt[maxe], t[maxe], len[maxe], que[maxn + 10], tim[maxn];
long long dis[maxn];
bool iq[maxn];

void adde(int a, int b, int c)
{
	t[++tot] = b; nt[tot] = h[a]; h[a] = tot; len[tot] = c;
}

bool spfa()
{
	int head, tail, p, q;
	for (int i = 0; i <= n; i++) 
	{
		iq[i] = 1;
		tim[i] = 1;
		que[i + 1] = i;
		dis[i] = 0;
	}
	tail = n + 1; head = 1;
	while (head <= tail)
	{
		p = que[head];	iq[p] = 0;		
		for(q = h[p]; ~q; q = nt[q])
			if (dis[p] + len[q] < dis[t[q]])
			{
				int tq = t[q];
				dis[tq] = dis[p] + len[q];
				if (!iq[tq])
				{
					++tail; if (tail > maxn) tail = 0;
					que[tail] = tq; iq[tq] = 1; 
					if (++tim[tq] > n + 2) return 0;
				}
			}
		++head; if (head > maxn) head = 0;
	}
	return 1;
}

int main()
{
   while (scanf("%d%d\n", &n, &m) != EOF)
   {
		tot = 0;
		memset(h, -1, sizeof h);
	   for (i = 1; i <= n; ++i)
		   scanf("%d", &c), adde(i - 1, i, c), adde(i, i - 1, 0);
	   while(m--) 
		   scanf("%d%d%d", &i, &j, &k), adde(j, i - 1, -k);
	   if (spfa()) printf("%d\n", - dis[0]);
			else puts("Bad Estimations");
   }
   return 0;
}


zoj2770(差分约束)火烧连营 差分约束详细解释见电子书 例 4.13 火烧连营(Burn the Linked Camp) 题目来源:ZOJ Monthly, October 2006, ZOJ2770 题目描述: 大家都知道,三国时期,蜀国刘备被吴国大都督陆逊打败了。刘备失败的原因是刘备的错误 决策。他把军队分成几十个大营,每个大营驻扎一队部队,又用树木编成栅栏,把大营连成一片, 称为连营。 让我们回到那个时代。陆逊派了... 阅读详情

相关推荐

POJ1275-Cashier Employment【差分约束系统】

正题 题目链接:http://poj.org/problem?id=1275 题目大意 1∼241\sim 241∼24小时中第iii个小时需要rir_iri​个出纳员 有nnn个人应聘,第iii从xix_ixi​开始工作,一直工作8个小时。 求至少要招募多少人应聘。 解题思路 numinum_inumi​表示第iii个小时有多少人招聘。 设定kik_iki​表示第iii个小时放多少人 这时需...

QuantAsk 518

POJ 1275 Cashier Employment(差分约束系统)

摘自冯威论文——《数与图的完美结合》设num[ i ]为i时刻能够开始工作的人数,x[ i ]为实际雇佣的人数,那么x[ I ]设r[ i ]为i时刻至少需要工作的人数,于是有如下关系:    x[ I-7 ]+x[ I-6 ]+x[ I-5 ]+x[ I-4 ]+x[ I-3 ]+x[ I-2 ]+x[ I-1 ]+x[ I ]>=r[ I ] 设s[ I ]=x[ 1 ]+x[ 2 ]…+x[ I ],得到    0    s[ I ]-s[ I-8 ]>=r[ I ], 8    s[ 23 ]+s

Czy 2251

差分约束系统学习笔记

躺在前面不得不说厉害的人真的很多,有些题不看题解全靠me自己做真的太难了。me还是不够强啊。差分是个什么呢?差分就是,题目给定一堆要求,这些要求在转化后可以表示成多个A - B ≥ val 或者A - B ≤ val 组合起来的不等式组 构成这个不等式组的不等式,一般左边是两个状态(两个待求值),右边是一个定值(一般是已知值),然后再通过模型转化,转化成最长路或者最短路问题求解举个栗子给定一个长度

泉華子的OI足迹 636

ZOJ 1508 Intervals【差分约束

Intervals Time Limit: 10 Seconds      Memory Limit: 32768 KB You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: > reads the number

wyjwyl的专栏 532

zoj2770 差分约束

这个题考虑的细节真多,但做完后感觉自己对差分约束的理解再次加深了。 /* 用s[i]表示前i的军营的总人数 则s[j]-s[i-1]就是第i个军营到第j个军营的总人数 所以可以把s[i]看做一个节点,然后根据约束条件建边 此题的关键是把所有约束条件给抽象出来! 最后判断是否存在负环 */ #include #include using namespace std; const int N=1

程序猿之路 1372

ZOJ 2770】Burn the Linked Camp 差分约束 Bellman_Ford

Burn the Linked Camp Time Limit:2 Seconds Memory Limit:65536 KB It is well known that, in the period of The Three Empires, Liu Bei, the emperor of the Shu Empire, was defeated by Lu Xun, a ge...

天才小熊猫的博客 285

差分约束

今天我们说一下差分约束差分约束是什么呢?差分约束差分约束系统, 如果一个系统由n个变量和m个约束条件组成,形成m个形如ai-aj≤k的不等式(i,j∈[1,n],k为常数),则称其为差分约束系统(system of difference constraints)。亦即,差分约束系统是求解关于一组变量的特殊不等式组的方法。(摘自百度百科) 举个例子,现在有五个变量v0,v1,v2,v3,v...

hhhcbw的博客,欢迎各位来访 553

zoj 1508 Intervals 差分约束系统

题目大意:输入n个区间,每个区间有3个值,ai,bi,ci代表在区间[ai,bi]上至少要有ci个整数点,现在要满足所有区间的自身条件,问最少选多少个点。 类似zoj 2770可以建模成一个差分约束系统. #include <stdio.h> #include <string.h> int n,dist[50010]; struct e {...

amily05200的博客 122

差分约束系统详解

差分约束问题差分约束系统 原理 建图 求最小解 求最大解差分约束问题差分约束系统给出一些形如x-y<=b不等式的约束,问是否有满足问题的解,或者求最小,最大解。 这个问题的神奇之处是可以转化为图论的最短路问题。原理对于图论的最短路径,有:对于d(v) <= d(u) + w(u, v) ,而差分约束系统的解法利用到了单源最短路径问题中的三角形不等式。 移项得:d(v) – d(u) <= w(u

DZYO的博客 473

zoj 2770 差分约束

1 /* 2 题意:给出N个营地,每个营地最多可容纳Ci人,给出m个三元组i,j,k分别表示当前营地从i到j至少有 3 k个人,问N个营地总共至少有多少人 4 5 题解:差分约束(SPFA) 6 题目中给出的很多条件都是约束的条件,例如最多有Ci个人,最少有k个人,问的问题也同样是最少多 7 少人,当所有给出的都是限制条件,然后自然会写出一些...

dianxie7120的博客 184

ZOJ - 2770 差分约束

题意:n个军营,给出每个军营的最大人数,m个估计,表示一段区间内的军营的最少人数和,现在让你求所有军营的最小可能人数和利用前缀和构造不等式关系,形成差分约束系统,其实就是跑一遍最短路就可以了#include&lt;bits/stdc++.h&gt; #define inf 0x3f3f3f3f3f3f3f3f using namespace std; const int maxn = 2000;...

jerans的博客 375

ZOJ2770,火烧连营,差分约束

差分约束关键在于明白为什么可以转化为三角不等式。 还有对于不等式   Xi - Xj 。 这ZOJ2770自己分析的还是挺正确的。 具体分析不写了,附个代码= =  加注释可能稍微比较乱。 #include #include #include #include #include #include #include #include #include #include

Beyyes 1597

ZOJ1508 (差分约束

Intervals Time Limit: 10 Seconds      Memory Limit: 32768 KB You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: > reads the number of in

Dreamer_WishSky的专栏 694

zoj2770,差分约束系统

差分约束系统,spfa判负环。

糖果屋 614

ZOJ2770 Burn the Linked Camp 差分约束

这是一道 最短路+差分约束 差分约束我还不是很熟悉。但就是通过题目中给予我们的不等关系来写出不等式,这里不等式的形式是(xi-xj,他的权值是a,然后xi,和xj就分别代表最短路中从源点到点i(j)的最短路劲。然后我们在自己加入一个源点就可以吧差分约束的类型的题目转化成为一道最短路径的题目。 以这道题目为例子。 题目中给了我们每一个军营的上限,和从第i到第j个军营的人数下限,从而我们可以写出

870

ZOJ】2770 Burn the Linked Camp 差分约束

传送门:【ZOJ】2770 Burn the Linked Camp 题目分析: 这题差分约束相对简单了不少啊。 gen j

poursoul 805

zoj 1508 Intervals (差分约束)

IntervalsTime Limit:10 Seconds Memory Limit:32768 KB You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.Write a program that:> reads the number of intervals,...

anque1234的博客 179
上一篇: 【专题】树
下一篇: 【FJOI2007】轮状病毒,以及一类普通的生成树计数
emoizhang
博客等级 码龄14年 7粉丝 41原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值