poj 1201 Intervals(差分约束系统)(中等)

Intervals
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23205 Accepted: 8764

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 end points 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 <= 50000) -- 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 <= 50000 and 1 <= ci <= bi - ai+1.

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

思路:
开始想着用贪心解这题,但模型感觉有点负复杂,不好处理。然后就用差分约束系统解。s[i]表示集合Z中小于等于i的元素个数。思路详解见 《图论算法理论,实现及应用》。
一下摘一段写的比较好的思路:

设s[x] = 从0 到x 的所有在集合中的数的个数

则ai到bi的个数即S[bi] - S[ai-1]。
因此有

(1) S[bi] - S[ai-1] >= ci。

又根据s[x]本身的性质,后面的一定不比前面的小,后面的最多比前面多一,有:
(2)  s[i + 1] - s[i] >= 0 
(3)  s[i + 1] - s[i] <= 1
故建图,使图中每一组边,均满足(注意三条式子的不等号方向要一致,这个很重要):
S[ai - 1] <= S[bi] - ci 
S[i] <= S[i - 1] + 1 
S[i - 1] <= S[i]

上面三式,可把s[x]看作源点(假设存在)到各点的最短距离,初始化为0;

常数为边(ai – 1,bi)的边权


当存在不满足这三条式子的边时,对这条边进行Relax操作,更新不等号左边的变量

其实就是Bellman-Ford算法的核心部分

if( S[ai - 1] > S[bi] – 2 )   S[ai - 1] = S[bi] – ci ;

if( S[i] > S[i - 1] + 1 )   S[i] > S[i - 1] + 1 ;

if( S[i - 1] > S[i] )   S[i - 1] = S[i] ;

 

最后源点到最大顶点的距离减去源点到最小顶点的距离就是所求(其实一个单位距离就代表V中的一个元素;最小顶点到最大顶点其实就是所有输入的区间中,最小的左端点到最大的右端点这个范围)。


代码:
注意,把权值为负的全部修改为正的,其余相应调整,否则会超时。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define inf 99999
#define maxx 50005
struct edge
{
    int u,v,w;
}edge[maxx];
int n;
int dist[maxx];
int l;//左端点的最小值
int r;//右端点的最大值

void bellman_ford()
{
    int flag=1,t;
    while(flag)
    {
        flag=0;
        for(int i=1;i<=n;i++)
        {
            t=dist[edge[i].u]+edge[i].w;
            if(dist[edge[i].v]>t)
            {
                dist[edge[i].v]=t;
                flag=1;
            }
        }
        for(int i=r;i>l;i--)
        {
            t=dist[i-1]+1;
            if(dist[i]>t)
            {
                dist[i]=t;flag=1;
            }
        }
        for(int i=r;i>l;i--)
        {
            t=dist[i];
            if(dist[i-1]>t)
            {
                dist[i-1]=t;
                flag=1;
            }
        }
    }
}

int main()
{
    scanf("%d",&n);
    r=1,l=inf;
    int a,b,c;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        edge[i].u=b,edge[i].v=a-1,edge[i].w=-c;
        if(a<l)  l=a;
        if(b>r)    r=b;
        dist[i]=0;
    }
    bellman_ford();
    printf("%d\n",dist[r]-dist[l-1]);
    return 0;
}
已AC代码:
//Memory Time
//996K  1141MS

#include<iostream>
using namespace std;

const int inf=60000;

class
{
	public:
		int s,e;
}inter[50001];

int n; //区间数
int upli;
int doli; // UpLimit , Downlimit 上下限
int dist[50001];  //源点到各点的距离
int c[50001];  //边权

int main(int i,int j,int k)
{
	while(cin>>n)
	{
		upli=0;
		doli=inf;

		/*Input*/

		for(k=0;k<n;k++)
		{
			int a,b;
			cin>>a>>b>>c[k];
			inter[k].s=a;
			inter[k].e=b+1;

			if(doli>inter[k].s)  //寻找最小的顶点
				doli=inter[k].s;
			if(upli<inter[k].e)  //寻找最大的顶点,inter[k].e必大于inter[k].s,因此无需再与inter[k].s比较
				upli=inter[k].e;

			dist[k]=0; //初始化源点到各点的距离
		}

		/*Bellman-Ford:Relax*/

		bool flag=true;
		while(flag)
		{
			flag=false;
			for(i=0;i<n;i++)
				if(dist[ inter[i].s ]>dist[ inter[i].e ]-c[i])
				{
					dist[ inter[i].s ]=dist[ inter[i].e ]-c[i];
					flag=true;
				}

			for(j=doli;j<upli;j++)
				if(dist[j+1]>dist[j]+1)
				{
					dist[j+1]=dist[j]+1;
					flag=true;
				}

			for(j=upli-1;j>=doli;j--)
				if(dist[j]>dist[j+1])
				{
					dist[j]=dist[j+1];
					flag=true;
				}
		}
		cout<<dist[upli]-dist[doli]<<endl;
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值