差分约束系统+poj1201

Language:
Intervals
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19858 Accepted: 7509

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

题意:给你一些区间,【a,b】从中至少取c个整数点,问最后最少需要取多少点。
思路:又是一个差分约束系统,前一段时间做过一个,但还是不太熟悉。
将区间[ai,bi]的每个端点看做一个顶点,以dist[i+1]表示源点xi之间至少有与集合Z相同元素的个数。如果将至少含有的相同元素的个数看做边的权值则一个区间[ai,bi]的两个端点的dist[ai]dist[bi+1]分别表示源点xai-1bi的距离。那么区间[ai,bi+1]又可以看做什么呢?可以看做aibi的一条边。依据题意 xaibi构成了一个边权三角形dist[bi+1]-dist[ai]>=ai->bi,即dist[bi+1]-dist[ai]>=ci;所以变形一下就得:dist[bi+1]>=dist[ai]+ci;注意:这个结果只是题目的要求,所以我们if(dist[bi+1]<dist[ai]+ci) dist[bi+1]=dist[ai]+ci;这也决定了求的是最长路径。

如果只有题目所给的约束条件的话,是远远不够的。所以还需要挖掘隐藏的条件:0<=dist[i+1]-dist[i]<=1。有了这个条件我们便可以建立数据结构了,然后我们以出现的最小元素min1为源点spfadist[max1]就是解了。

又看了一下什么时候取最小值,什么时候取最大值,d[v] <= d[u] + w(u, v), 初始化d[]为-INF. 这样求出的d[]就是满足条件的最小值。原因很简单,因为d[i] 是从-INF开始增大,根据不等式逐渐增大,当满足所有不等式时,那么d[i]肯定是最小的了。d[v] >= d[u] + w(u, v), 初始化d[]为INF.这样求出的d[]就是最大值。原因和上面一样,因为是从INF逐渐减小的,当满足完所有条件时,就停止。那么d[i]是最大的了。 下面是代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=50050;
const int MAXN=200000;
const int INF=1000000000;
struct node
{
    int v,next,c;
}e[MAXN];
int pre[maxn],dis[maxn];
bool vis[maxn];
int N,num;
int s,en;
void init()
{
    memset(pre,-1,sizeof(pre));
    num=0;
}
void add_edge(int x,int y,int c)
{
    e[num].v=x;
    e[num].next=pre[y];
    e[num].c=c;
    pre[y]=num++;
}
int SPFA()
{
    for(int i=s;i<=en;i++)
    {
        dis[i]=-INF;
        vis[i]=false;
    }
    queue<int> q;
    q.push(s);
    dis[s]=0;
    while(!q.empty())
    {
        int t=q.front();
        q.pop();
        vis[t]=false;
        for(int i=pre[t];i!=-1;i=e[i].next)
        {
            int v=e[i].v;
            if(dis[v]<dis[t]+e[i].c)
            {
                dis[v]=dis[t]+e[i].c;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=true;
                }
            }
        }
    }
    return dis[en];
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
    #endif

    while(scanf("%d",&N)!=EOF)
    {
        int a,b,c;
        init();
        s=INF;en=-INF;
        for(int i=0;i<N;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add_edge(b+1,a,c);
            s=min(s,a);
            en=max(en,b+1);
        }
        for(int i=s;i<en;i++)
        {
            add_edge(i+1,i,0);
            add_edge(i,i+1,-1);
        }
        cout<<SPFA()<<endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值