POJ 1201 Intervals (区间差分约束)


Intervals
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 27261 Accepted: 10465

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


这题数据量笑点可以贪心做 : http://blog.csdn.net/qq_34374664/article/details/72179423

题意:

第一行输入n,下面输入n个限制条件,条件的格式为 ai bi ci,  0<=ai<=bi<=50000,1<=ci<=bi-ai+1.表示在线段[ai,bi]上至少选ci个点,使被选出的点的个数最少而且满足所有的限制条件,输出这个最小值

思路:典型的区间差分约束, 维护区间之间的约束之外, 要保证 1 => s[i]-s[i-1] >= 0,因为这s代表的是前缀和,如果出线前面前缀和比后面要大那就是错的,所以要加这个约束

求最小值, 所以就是最长路....这题一定是连通的,因为第二个约束已经连通了,如果不联通就要写一个源点~


网上比较好的题解:

  令s[x]表示从区间[0,x]中选择的整数点个数.那么对于条件[ai, bi]选数>=ci个,就是 s[bi]-s[ai-1]>=ci. 即s[ai-1]<=s[bi]+ (-ci)

       假设有下面两个条件:

1 2 3  5 6 3, 那么我们自然得出了s[2]-s[0]>=3 且s[6]-s[4]>=3.

如果s[2]=3,s[0]=0,s[6]=3,s[4]=0 是满足上面不等式的.但是其实是不合题意的.因为s[4]必然要>=s[2]和s[0]的.所以我们需要把s的相对值也联系起来.

       这样就需要添加下面的边. 假设我们令输入读取的区间最大值为max_v. 那么对于所有 0<i<=max_v的值来说,有

       0<=s[i]-s[i-1]<=1 .转换一下即是: s[i]<=1+s[i-1] && s[i-1]<=0+s[i].

       根据上面的分析,我们要建的有向图是一个具有max_v+1个点的图.其中的边有:

       n条s[bi]-s[ai-1]>=ci条件构成的从bi (ai-1)的长-ci的边.

还有所有 0<i<=max_v的值构成的 ii-1的长0的边 i-1i的长1的边.所以这样我们就形成了一个具有点[0,max_v]的有向图.在原图处理时,为了避免ai-1==-1,我们令所有ai与bi都自增了10.所以我们形成了一个具有点[9,max_v]的有向图(其实就是差分约束系统).我们的目标是令S[max_v]-S[9]的值最小.(切记这里不是仅使S[max_v]的值最小,因为我们这里只有从9 ,10,11,…max_v 的值构成了一个差分约束系统.max_v的值和0号点的值是不在一个系统的. 0号点是超级源点,它的值与系统中其他点的值是无关的.)

       然后我们上面已经知道了希望让系统中S[max_v]和S[9]的值差距尽量小.(构成差分约束系统时,1.如果在所有点外添加一个超级源0号点,并使得超级源到所有其他点的距离为0,那么最终求出的0号点到其他所有原始点的最短距离就是本系统的一个可行解,且可行解之间的差距最小.      2.如果初始时不添加超级源,只是将原始点的初始距离设为INF,且令其中一个原始点的初始距离为0,然后求该点到其他所有点的最短距离,那么最短距离的集合就是一个可行解,且该可行解两两之间的差距最大.注意方案2只能在该问题一定存在解的时候即肯定不存在负权环的时候用.否则从1号点到其他点没有路,但是其他点的强连通分量中有负权环,这样根本探测不到错误) 所以我们需要采取方案1.



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int INF = 2e9;
const int maxn = 5e4 + 5;
int n, m, x, y, d, e;
struct node
{
    int to, w;
    node(){}
    node(int tt, int ww) : to(tt), w(ww){}
};
vector<node> v[maxn];
int book[maxn], cnt[maxn], dis[maxn];
void spfa(int s)
{
    memset(book, 0, sizeof(book));
    for(int i = 0; i < maxn; i++) dis[i] = INF;
    queue<int> q;
    q.push(s);
    book[s] = 1;
    dis[s] = 0;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        book[u] = 0;
        for(int i = 0; i < v[u].size(); i++)
        {
            int to = v[u][i].to;
            if(dis[u]+v[u][i].w < dis[to])
            {
                dis[to] = dis[u] + v[u][i].w;
                if(!book[to])
                {
                    book[to] = 1;
                    q.push(to);
                }
            }
        }
    }
    printf("%d\n", -dis[e]);
    return ;
}
int main()
{
    while(~scanf("%d", &n))
    {
        memset(book, 0, sizeof(book));
        memset(cnt, 0, sizeof(cnt));
        for(int i = 0; i < maxn; i++)
            v[i].clear();
        int s = INF;
        e = -1;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d", &x, &y, &d);
            x++, y++;
            s = min(s, x-1);
            e = max(e, y);
            v[x-1].push_back(node(y, -d));
        }
        for(int i = s; i < e; i++)
        {
            v[i+1].push_back(node(i, 1));
            v[i].push_back(node(i+1, 0));
        }
        spfa(s);
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值