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

题目大意:

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(图论-差分约束)的更多相关文章

  1. POJ 1201 Intervals【差分约束】

    传送门:http://poj.org/problem?id=1201 题意: 有n个如下形式的条件:,表示在区间[, ]内至少要选择个整数点.问你满足以上所有条件,最少需要选多少个点? 思路:第一道差 ...

  2. poj 1201 Intervals(差分约束)

    做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...

  3. POJ 1201 Intervals (经典) (差分约束)

    <题目链接> 题目大意:给你$n$段区间,$a_i,b_i,c_i$ 表示在 $[a_i,b_i]$ 区间内至少要选择$c_i$个点.现在问你在满足这n个条件的情况下,最少要选多少个点? ...

  4. 【题解】 POJ 1201 Intervals(差分约束)

    懒得复制,戳我戳我 Solution: 这道题就是一个板子题 抽象成第\(a\)至第\(b\)间选择数的个数为\(c\),我们就可以用前缀和来表示,这样就可以得到不等式\(s[b]-s[a-1]> ...

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

    关于差分约束详情可阅读:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 题意: 给定n个区间[L,R], 每个区间至 ...

  6. poj 1201 Intervals【差分约束+spfa】

    设s为前缀和,首先显然的条件是\[ s_{bi}-s_{ai-1}>=c \],然后隐含的是\[ s_i-s_{i-1}>=0 s_i-s_{i-1}<=1 \] 然后根据差分约束, ...

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

    题意: 有一个集合Z,其元素都是整整数,但是数量未知.现有n个约束,形如 [a,b]=c 表示整数区间[a,b]中有c个元素在Z中出现.问集合Z最小可能含多少个元素? 思路: 对于所给的区间 cnt[ ...

  8. poj 1201 Intervals(差分约束)

    题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...

  9. poj 1201 Intervals——差分约束裸题

    题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...

  10. POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束

    POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...

随机推荐

  1. 很受欢迎的Linux笔记(短小精悍)

    http://blog.csdn.net/xsl1990/article/details/8274028 如何知道所使用的LINUX是哪个发行版? lsb_release -a 查找某个文件的另类方法 ...

  2. VCL+FMX 双剑合壁编程

    VCL 是经典,FMX 是新生,新生事物总会带来一些好玩新奇的东西.舍弃经典是浪费,不了解新生事物是等死,那么我们来一个二合一双剑合壁又如何呢? 要双剑合壁,就得投些机,取些巧.由于 Delphi / ...

  3. JS 中Promise 模式

    异步模式在web编程中变得越来越重要,对于web主流语言Javscript来说,这种模式实现起来不是很利索,为此,许多Javascript库(比如 jQuery和Dojo)添加了一种称为promise ...

  4. Windows系统性能提升方法

    看前提醒:在确认没有病毒和流氓软件的前提下,建议优化电脑:以下操作已经在Win7上试验,Win7以上的园友自己试验,自己感受,对电脑无害,但操作时请务必小心 设置虚拟内存 虚拟内存最小值物理内存1.5 ...

  5. Oracle系列之游标

    涉及到表的处理请参看原表结构与数据  Oracle建表插数据等等 游标: 1.目的 解决“ select * ”返回空.多行记录问题 但凡select,就可能多行结果集,也就需要用游标 2.原理 多行 ...

  6. 一类最小割bzoj2127,bzoj2132 bzoj3438

    思考一下我们接触的最小割问题 最小割的基本问题(可能会和图论的知识相结合,比如bzoj1266,bzoj1797) 最大权闭合图(bzoj1497) 最大点权覆盖集,最大点权独立集(bzoj1324) ...

  7. jquery easyui treegrid使用小结

    在实际应用中可能会碰到不同的需求,比如会根据每行不同的参数或属性设置来设置同列不同的editor类型,这时原有的例子就显的有点太过简单,不能实现我们的需求,现在应用我在项目中的操作为例,显示下实现同列 ...

  8. c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号

    Write a program to compare two files, printing the first line where they differ. Here's Rick's solut ...

  9. html的两种提交按钮submit和button

    转自:http://baiying.blog.51cto.com/1068039/1319784 html按钮有两种: <input type="button" value= ...

  10. 关于ButterKnife 8.1.0使用遇到的问题

    ButterKnife注解方式 和eventbus 差不多 都很好用 @OnClick(R.id.button) void onButtonClick() { //TODO implement Toa ...