POJ 1716 Integer Intervals

Description
An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b.
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.


【题目分析】
同样是差分约束系统。限制条件见代码,共三种。


【代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
int h[400001],fr[400001],ne[400001],to[400001],w[400001],en=0,d[400001];
int inq[400001];
int n,m,maxx;
inline void add(int a,int b,int c)
{to[en]=b;ne[en]=h[a];fr[en]=a;w[en]=c;h[a]=en++;}
inline void bfs()
{
    for (int i=0;i<=maxx;++i) d[i]=-inf;
    queue<int>q;
    q.push(0);
    inq[0]=1;d[0]=0;
    while (!q.empty())
    {
        int x=q.front();q.pop();
        inq[x]=0;
        for (int i=h[x];i>=0;i=ne[i])
        {
            if (d[x]+w[i]>d[to[i]])
            {
                d[to[i]]=d[x]+w[i];
                if (!inq[to[i]])
                {
                    q.push(to[i]);
                    inq[to[i]]=1;
                }
            }
        }
    }
    cout<<d[maxx]<<endl;
}
int main()
{
    cin>>n;
    maxx=-1;
    memset(h,-1,sizeof h);
    for (int i=1;i<=n;++i)
    {
        int a,b;
        cin>>a>>b;
        add(a,b+1,2);
        maxx=max(maxx,b+1);
    }
    for (int i=0;i<maxx;++i)
    {
        add(i,i+1,0);
        add(i+1,i,-1);
    }
    bfs();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值