【差分约束】zoj两题

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

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值