hdoj 1384 Intervals 【差分约束基础题目】

本文介绍了一道关于差分约束系统的编程题,通过构建图模型并利用SPFA算法求解最小覆盖集的大小。文章详细解释了题目的含义,并提供了一种有效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Intervals

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3211    Accepted Submission(s): 1181


Problem Description
You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.

Write a program that:

> reads the number of intervals, their endpoints and integers c1, ..., cn from the standard input,

> computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i = 1, 2, ..., n,

> writes the answer to the standard output
 

Input
The first line of the input contains an integer n (1 <= n <= 50 000) - the number of intervals. The following n lines describe the intervals. The i+1-th line of the input contains three integers ai, bi and ci separated by single spaces and such that 0 <= ai <= bi <= 50 000 and 1 <= ci <= bi - ai + 1.

Process to the end of file.

 

Output
The output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai, bi], for each i = 1, 2, ..., n.
 

Sample Input
5 3 7 3 8 10 3 6 8 1 1 3 1 10 11 1
 

Sample Output
6
 
刚学差分约束,不敢玩高大尚的,一步步来。

题意:有n个区间,给出每个区间的左端点和右端点以及在该区间至少有多少个数在集合Z里面,现在让你求出集合Z里面最少元素个数。

思路:设S[i] 表示集合Z里面的元素在区间[0, i ]的个数,Maxl,Maxr分别表示所有区间里面的最左端和最右端,dist[]数组存储源点到某点的最短路。则由题意得限制条件
一 S[right] -  S[left-1] >= least 即[left, right]区间个数不小于least,转换得S[left-1] - S[right] <= least;
二 0 <= S[i] - S[i-1] <= 1转换得 S[i-1] - S[i] <= 0 && S[i] - S[i-1] <= 1。
然后根据限制条件建图


转化问题:题目需要求的是S[Maxr] - S[Maxl-1] >= ans 即S[Maxl-1] - S[Maxr] <= -ans。 若以Maxr为源点 ,而-ans就为Maxr到Maxl-1的最短路径的相反数,即-dist[Maxl-1]。

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 1000000000
#define MAX 50000+10
using namespace std;
struct Edge
{
	int from, to, val, next;
}edge[150000+10];
int n;//n个区间
int dist[MAX];//最短路 以区间最右端为源点 
int Maxl, Maxr;//区间最左段与最右端 
bool vis[MAX];//标记是否在队列里面 
int head[MAX], top;  
void init()
{
	top = 0;
	memset(head, -1, sizeof(head));
	Maxl = 50000+10, Maxr = 0;
}
void addedge(int u, int v, int w)
{
	Edge E = {u, v, w, head[u]};
	edge[top] = E;
	head[u] = top++;
} 
void getMap()
{
	int a, b, c;
	for(int i = 0; i < n; i++)
	{
		scanf("%d%d%d", &a, &b, &c);
		Maxl = min(Maxl, a);
		Maxr = max(Maxr, b);//更新 
		addedge(b, a-1, -c);
	}
	for(int i = Maxl; i <= Maxr; i++)
	{
		addedge(i, i-1, 0);
		addedge(i-1, i, 1);
	} 
} 
void SPFA()
{
	queue<int> Q;
	for(int i = Maxl-1; i <= Maxr; i++)//区间最右端为源点 
	dist[i] = i==Maxr ? 0 : INF; 
	memset(vis, false, sizeof(vis));
	vis[Maxr] = true;
	Q.push(Maxr);
	while(!Q.empty())
	{
		int u = Q.front();
		Q.pop();
		vis[u] = false;
		for(int i = head[u]; i != -1; i = edge[i].next)
		{
			Edge E = edge[i];
			if(dist[E.to] > dist[u] + E.val)
			{
				dist[E.to] = dist[u] + E.val;
				if(!vis[E.to])
				{
					vis[E.to] = true;
					Q.push(E.to);
				} 
			}
		}
	}
	printf("%d\n", -dist[Maxl-1]);
}
int main()
{
	while(scanf("%d", &n) != EOF)
	{
		init();
		getMap();
		SPFA();
	}
	return 0;
}












评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值