POJ 1201 Intervals(图论-差分约束)

Intervals

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20779   Accepted: 7863

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

题目大意:

n行,每行a,b,c,表示在区间a,b内要找c个数,问你总共至少要找多少个数?

解题思路:

差分约束系统。

在本题中,如果[a,b]中要找c个元素,那么:s[b]-s[a-1]>=c,我们可以推得:s[a-1] - s[b] <= -c

同时,由于每一个值上最多只能含有一个元素,那么:s[i] - s[i-1]<=1 ,又由于s[i] - s[i-1]>=0 推得:s[i-1] - s[i] <=0

这样:我们有了三个约束不等式:

s[a-1] - s[b] <= -c

s[i] - s[i-1]<=1

s[i-1] - s[i] <=0

于是:如果起点为from,重点为to,我们只要求出:s[to] -s[from-1] >= M就可以了,

因此求出 s[from-1]-s[to]<=-M,即求to 到 from-1 的最短路径,

注意:由于i<0,所以i-1可能小于0,因此全部向右平移1位。

解题代码:

#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;

const int maxn=50000;
const int inf=0x3f3f3f3f;

struct edge{
    int u,v,w,next;
}e[maxn*10];

int head[maxn*2+10],dist[maxn*2+10],cnt;
int n,from,to;

void initial(){
    cnt=0;
    from=inf,to=0;
    for(int i=0;i<=maxn;i++) head[i]=-1;
}

void adde(int u,int v,int w){
    u++;v++;
    e[cnt].u=u,e[cnt].v=v,e[cnt].w=w,e[cnt].next=head[u],head[u]=cnt++;
}

void input(){
    int u,v,w;
    //[v]-[u-1]>=w [u-1]-[v]<=-w
    for(int i=0;i<n;i++){
        scanf("%d%d%d",&u,&v,&w);
        adde(v,u-1,-w);
        if(u<from) from=u;
        if(v>to) to=v;
    }
    //0<=[i]-[i-1]<=1
    for(int i=from;i<=to;i++){
        adde(i-1,i,1);
        adde(i,i-1,0);
    }
}

bool spfa(int from){
    int s=from,num[maxn];
    bool visited[maxn];
    for(int i=0;i<=maxn;i++){
        num[i]=0;
        dist[i]=inf;
        visited[i]=false;
    }
    queue <int> q;
    q.push(s);
    visited[s]=true;
    dist[s]=0;
    while(!q.empty()){
        s=q.front();
        q.pop();
        for(int i=head[s];i!=-1;i=e[i].next){
            int d=e[i].v;
            if(dist[d]>dist[s]+e[i].w){
                dist[d]=dist[s]+e[i].w;
                if(!visited[d]){
                    visited[d]=true;
                    q.push(d);
                    num[d]++;
                    if(num[d]>n) return false;
                }
            }
        }
        visited[s]=false;
    }
    return true;
}

void solve(){
    //get [to]-[from-1]>=M; [from-1]-[to]<=-M
    spfa(to+1);
    cout<<-dist[from]<<endl;
}

int main(){
    while(scanf("%d",&n)!=EOF){
        initial();
        input();
        solve();
    }
    return 0;
}

POJ 1201 Intervals(图论-差分约束),布布扣,bubuko.com

时间: 2024-08-07 07:45:40

POJ 1201 Intervals(图论-差分约束)的相关文章

POJ 1201 Intervals(差分约束 区间约束模版)

关于差分约束详情可阅读:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 题意: 给定n个区间[L,R], 每个区间至少放w个球, 问最后整个区间最少要放多少个球. 分析: 假设d[i] 是 [1,i] 至少有多少个点被选中, 特殊地, d[0] = 0. 每个区间的描述可以转化为d[R] - d[L-1] >= w.(因为d[L]也要选中, 左闭右闭区间, 所以要减d[L-1])因为d[i]描述了一个求和函数,所

poj 1201 Intervals【差分约束+spfa】

设s为前缀和,首先显然的条件是\[ s_{bi}-s_{ai-1}>=c \],然后隐含的是\[ s_i-s_{i-1}>=0 s_i-s_{i-1}<=1 \] 然后根据差分约束,就是连边(bi,ai-1,-li),(i-1,i,1),(i,i-1,0) spfa跑最长路最后输出相反数即可,注意n是起点,min是终点,跑最短路(不会有负环) #include<iostream> #include<cstdio> #include<queue> usi

POJ 1201 Intervals(差分约束)

[题目链接] http://poj.org/problem?id=1201 [题目大意] 告诉你一个区间至少要选定的数字的个数,给出n个区间的需求 问最少选取几个数字可以满足所有的需求 [题解] 对于区间[a,b]建立不等式Sb+1-Sa>=c,最后要求最小化Smax, 结合基础条件Si+1-Si>=0,Si-Si+1>=-1,构造差分约束系统求最长路就是答案. [代码] #include <cstdio> #include <algorithm> #includ

POJ 1201 Intervals(差分约束+spfa 求最长路径)

题目链接:http://poj.org/problem?id=1201 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, com

POJ 1201 Intervals (差分约束,最短路)

题意:假设有一个自然数区间[0,50000],要从中挑出一些自然数出来,但是现在不知道整个区间究竟要挑多少个出来,只知道一部分闭区间[a,b]内至少要挑多少个,所知道的有n个闭区间.问至少要挑出多少个? 思路: 对于所给的区间 cnt[b-a]>=k这可以保证了该区间内个数不少于k.但是由于两边都是闭区间,所以要变cnt[b-(a-1)]>=k,表示b到a之间的个数.也就是说,转成式子是b-(a-1)>=k,变换一下为(a-1)-b<=-k,就满足常见的式子b-a<=k啦,可

POJ 3169 Layout (图论-差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6574   Accepted: 3177 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

poj 3169 Layout(差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 3168 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

uva 11478 Halum(图论-差分约束)

  Problem H Halum Time Limit : 3 seconds   You are given a directed graph G(V,E) with a set of vertices and edges. Each edge (i,j) that connects some vertex i to vertex j has an integer cost associated with that edge. Define the operation Halum(v, d)

POJ 1201 Intervals 差分约束

http://poj.org/problem?id=1201 TLE了很久,因为用了cin..... 思路和其他差分约束差不多,http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 如果区间[a, b]中至少有c个元素,如果用上面的博客,那么说明xa - xb >= c,但是注意这里是闭区间,xa - xb是不包括b这个点的, 就比如用了[a, b]有c个元素,[b, d]有x个,那么ans = c + x - 1个,