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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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 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


你可能感兴趣的:(poj)