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个闭区间,形如[ai,bi],在这n个区间的每一个整点位置放东西,每个区间至少要放ci个,求总的最少要放的个数
了解过查分约束系统之后,比较容易知道是差分系统的最小差问题,那么就是找全形如 y-x>=k的不等式,然后再求一个最长路
关键是建图问题:p[i]表示1~i放的东西个数,那么对于每一个区间,就要满足p[bi] - p[ai-1]>=ci,即拉一条(ai-1)->bi的有向边
但是有一个隐藏的约束条件,由于是整点放,那么p[x] - p[x-1] <= 1 && p[x] - p[x-1] >= 0  即p[x-1]-p[x]>=-1 && p[x]-p[x-1]>=0
另外,由于区间可以从0开始,那么把所有的区间整体右移2个位置,就是从1开始的了
 
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 5e4 + ;
struct Edge {
int v, cost;
Edge() {}
Edge(int v, int cost) : v(v), cost(cost) {}
};
vector<Edge> E[N];
void add(int u, int v, int w) {
E[u].push_back(Edge(v, w));
}
bool vis[N];
int d[N];
void SPFA(int st, int n) {
memset(vis, false, sizeof vis);
for(int i = ; i <= n; ++i) d[i] = -INF;
d[st] = ;
vis[st] = true;
queue<int> que;
while(!que.empty()) que.pop();
que.push(st);
while(!que.empty()) {
int u = que.front();
que.pop();
vis[u] = false;
int sx = E[u].size();
for(int i = ; i < sx; ++i) {
int v = E[u][i].v;
int w = E[u][i].cost;
if(d[v] < d[u] + w) {
d[v] = d[u] + w;
if(!vis[v]) {
vis[v] = true;
que.push(v);
}
}
}
}
}
int main() {
int n;
while(~scanf("%d", &n)) {
int a, b, c, m = , st = INF;
for(int i = ; i < n; ++i) {
scanf("%d%d%d", &a, &b, &c);
a+=; b+=;
add(a - , b, c);
m = max(m, b);
st = min(st, a - );
}
for(int i = ; i <= m; ++i) {
add(i, i - , -);
add(i - , i, );
}
SPFA(st, m);
printf("%d\n", d[m]);
}
return ;
}

POJ1201 Intervals差分约束系统(最短路)的更多相关文章

  1. POJ1201 Intervals[差分约束系统]

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26028   Accepted: 9952 Descri ...

  2. [BZOJ2330][SCOI2011]糖果 差分约束系统+最短路

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2330 类似于题目中这种含有不等式关系,我们可以建立差分约束系统来跑最长路或最短路. 对于一 ...

  3. Intervals(差分约束系统)

    http://poj.org/problem?id=1201 题意:给定n个整数闭区间[a,b]和n个整数c,求一个最小的整数集合Z,满足Z里边的数中范围在闭区间[a,b]的个数不小于c个. 思路:根 ...

  4. POJ 1201 Intervals (差分约束系统)

    题意 在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点. 思路 差分约束求最小值.把不等式都转换为&g ...

  5. POJ1201 Intervals(差分约束)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 10966 Description You ...

  6. poj1201 Intervals——差分约束

    题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...

  7. poj 1201/zoj 1508 intervals 差分约束系统

      // 思路 : // 图建好后 剩下的就和上一篇的 火烧连营那题一样了 求得解都是一样的 // 所以稍微改了就过了 // 最下面还有更快的算法 速度是这个算法的2倍#include <ios ...

  8. PKU 1201 Intervals(差分约束系统+Spfa)

    题目大意:原题链接 构造一个集合,这个集合内的数字满足所给的n个条件,每个条件都是指在区间[a,b]内至少有c个数在集合内.问集合最少包含多少个点.即求至少有多少个元素在区间[a,b]内. 解题思路: ...

  9. POJ1201 Intervals(差分约束系统)

    与ZOJ2770一个建模方式,前缀和当作点. 对于每个区间[a,b]有这么个条件,Sa-Sb-1>=c,然后我就那样连边WA了好几次. 后来偷看数据才想到这题还有两个隐藏的约束条件. 这题前缀和 ...

随机推荐

  1. 使用a标签删除进行提示

    一句话搞定: <a href="Login.aspx" target="mainFrame" class="STYLE4" oncli ...

  2. tableview详细介绍

    tableview详细介绍: https://www.baidu.com/link?url=MU5a5om66vYEKAcnvmXCeCwMGetezW5o2X11OUnwN7-fb_jWPx6xyv ...

  3. [Android Pro] Android保存图片到系统图库

    http://stormzhang.github.io/android/2014/07/24/android-save-image-to-gallery/ http://blog.csdn.net/x ...

  4. ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(九) 之 用 Redis 实现用户在线离线状态消息处理(一)

    前言 上一篇中简单讲解了用Redis缓存在线用户逻辑.篇幅也比较小,本篇将详细实现用户的上线下线通知.图片效果转换功能.而且,代码和开发思路都会详细介绍. 效果展示 目前有三个用户,user1,use ...

  5. Swift - as、as!、as?三种类型转换操作使用一览

    as.as!.as? 这三种类型转换操作符的异同,以及各自的使用场景.   1,as使用场合 (1)从派生类转换为基类,向上转型(upcasts) 1 2 3 4 class Animal {} cl ...

  6. 1.1-java创建包和类的方法

    1.new-package-命名方法com打头.中间名称.后台要创建的class 2.创建class-    new-class 选择一下主方法 代码示例  编译完保存一下就能输出信息,一直没有保存才 ...

  7. Android手绘效果实现

    效果图 原理 大概介绍一下实现原理.首先你得有一张图(废话~),接下来就是把这张图的轮廓提取出来,轮廓提取算法有很多,本人不是搞图像处理的,对图像处理感兴趣的童鞋可以查看相关资料.如果你有好的轮廓提取 ...

  8. 设置tomcat内存设定

    Linux: 在/usr/local/apache-tomcat-/bin 目录下的catalina.sh添加: JAVA_OPTS='-Xms512m -Xmx1024m'要加"m&quo ...

  9. android 入门-动画与容器

    set 动画容器 可作为资源id添加R.anim.xxxx   可用于在样式表中添加  http://blog.csdn.net/liuhe688/article/details/6660823 in ...

  10. NS2中trace文件分析

    ns中模拟出来的时间最终会以trace文件的形式告诉我们,虽然说一般都是用awk等工具分析trace文件,但是了解trace文件的格式也是必不可少的.下面就介绍一下无线网络模拟中trace文件的格式. ...