poj 1201Intervals (差分约束系统)

Intervals
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23864 Accepted: 9048

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

题意:

给出数轴上的n个区间[ai,bi],每个区间都是连续的int区间。现在要在数轴上任意取一堆元素,构成一个元素集合V要求每个区间[ai,bi]和元素集合V的交集至少有ci不同的元素求集合V最小的元素个数。

设Dis[i]为源点到达i集合中的元素个数

由题意可推出3个不等式:

Dis[bi] - Dis[ai-1] >= c[i];

Dis[i] - Dis[i-1] >= 0

Dis[i] - Dis[i-1] <= 1

根据上面三个关系建图,用SFPA求解最短路

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <vector>
#include <algorithm>
#include <iomanip>
#define RR freopen("in.txt","r"m,stdin)
#define WW freopen("out.txt","w",stdout)
#define LL long long
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 50010;
const double eps = 1e-9;
int Dis[MAXN],head[MAXN];
bool vis[MAXN];
int n,Left,Right,top;
typedef struct node
{
    int v,w,next;
}Edge;
Edge edge[3*MAXN];
void Add(int u, int v, int w)
{
    edge[top].v = v;
    edge[top].w = w;
    edge[top].next = head[u];
    head[u] = top++;
}
void SPFA()
{
    queue<int > Q;
    for(int i=Left; i<=Right; i++)
        Dis[i] = -INF;
    Dis[Left] = 0;
    Q.push(Left);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        vis[u] = false;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v = edge[i].v;
            int w = edge[i].w;
            if(Dis[v] < Dis[u] + w)
            {
                Dis[v] = Dis[u] + w;
                if(!vis[v])
                {
                    Q.push(v);
                    vis[v] = true;
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        top = 0;
        memset(head, -1, sizeof(head));
        memset(vis, false, sizeof(vis));
        memset(edge, 0, sizeof(edge));
        Left = MAXN;
        Right = 0;
        for(int i=0;i<n;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            Add(u,v+1,w);
            Left = min(Left, u);
            Right = max(Right, v+1);
        }
        for(int i=Left; i<Right; i++)
        {
            Add(i,i+1,0);
            Add(i+1,i,-1);
        }
        SPFA();
        printf("%d\n",Dis[Right]);
    }
    return 0;
}

/*
一组坑数据
输入:
8
4 9 5
8 9 1
5 6 1
3 9 5
7 9 1
2 5 2
3 4 1
2 5 3
*/

/*
输出:6
*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值