POJ 1201 Intervals 差分约束系统

Intervals
Time Limit: 2000MS      Memory Limit: 65536K
Total Submissions: 25735        Accepted: 9847

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

Source
Southwestern Europe 2002

考虑Si表示 < i的数中,取几个数
那[a,b]中取c个数—->S(b+1)-Sa>=c
又因为S(i+1)>=Si
S(i+1)-Si<=1
所以得到差分约束系统 满足:

Sa-S(b+1)<=-c
Si-S(i+1)<=0
S(i+1)-Si<=1

跑完SPFA发现 dist[i]<=0
若{x0,x1,…xn}为差分约束系统一个可行解
那对任意实数d
{x0+d,x1+d,….,xn+d}也是一个可行解
显然 让dist中的最小值>=0才是正解,所以dist[MAX]-min(dist)就是答案
而且显然本题中 dist[MAX]-min(dist)= -dist[1]

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

inline void read(int&x){
    x=0;
    char t;
    while(isdigit(t=getchar()))
        x=x*10+t-'0';
}

const int N=50000+5;
const int M=3*N;

int head[N];
struct Edge{
    int to,w,next;
}edge[M];

inline void addEdge(int k,int u,int v,int w){
    edge[k].to=v;
    edge[k].w=w;
    edge[k].next=head[u];
    head[u]=k;
}

int dist[N];
bool visit[N];
int outQue[N];

bool SPFA(int n){
    //fill(dist,dist+n+1,INF);
    fill(visit,visit+n+1,false);
    fill(outQue,outQue+n+1,0);
    deque<int>que;
    for(int i=1;i<=n;++i){
        que.push_back(i);
        visit[i]=true;
    }
    while(!que.empty()){
        int top=que.front();
        que.pop_front();
        visit[top]=false;
        outQue[top]+=1;
        if(outQue[top]>n+1)
            return false;
        for(int i=head[top];i!=-1;i=edge[i].next){
            int to=edge[i].to,
                    w=edge[i].w;
            if(dist[to]>dist[top]+w){
                dist[to]=dist[top]+w;
                if(!visit[to]){
                    visit[to]=true;
                    que.push_back(to);
                }
            }
        }
    }
    return true;
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int n,a,b,c,MAX=0;
    scanf("%d",&n);
    fill(head,head+N+1,-1);
    for(int i=0;i<n;++i){
        scanf("%d%d%d",&a,&b,&c);
        addEdge(i,b+1,a,-c);
        MAX=max(MAX,b);
    }
    for(int i=0;i<MAX;++i){
        addEdge(n+2*i,i+2,i+1,0);
        addEdge(n+2*i+1,i+1,i+2,1);
    }
    SPFA(MAX+1);
    printf("%d\n",-dist[1]);
    /*for(int i=1;i<=MAX;++i)
        cout<<dist[i]<<endl;*/
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值