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

  1. 5
  2. 3 7 3
  3. 8 10 3
  4. 6 8 1
  5. 1 3 1
  6. 10 11 1

Sample Output

  1. 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开始的了
 
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <stack>
  6. #include <vector>
  7. using namespace std;
  8. const int INF = 0x3f3f3f3f;
  9. const int N = 5e4 + ;
  10. struct Edge {
  11. int v, cost;
  12. Edge() {}
  13. Edge(int v, int cost) : v(v), cost(cost) {}
  14. };
  15. vector<Edge> E[N];
  16. void add(int u, int v, int w) {
  17. E[u].push_back(Edge(v, w));
  18. }
  19. bool vis[N];
  20. int d[N];
  21. void SPFA(int st, int n) {
  22. memset(vis, false, sizeof vis);
  23. for(int i = ; i <= n; ++i) d[i] = -INF;
  24. d[st] = ;
  25. vis[st] = true;
  26. queue<int> que;
  27. while(!que.empty()) que.pop();
  28. que.push(st);
  29. while(!que.empty()) {
  30. int u = que.front();
  31. que.pop();
  32. vis[u] = false;
  33. int sx = E[u].size();
  34. for(int i = ; i < sx; ++i) {
  35. int v = E[u][i].v;
  36. int w = E[u][i].cost;
  37. if(d[v] < d[u] + w) {
  38. d[v] = d[u] + w;
  39. if(!vis[v]) {
  40. vis[v] = true;
  41. que.push(v);
  42. }
  43. }
  44. }
  45. }
  46. }
  47. int main() {
  48. int n;
  49. while(~scanf("%d", &n)) {
  50. int a, b, c, m = , st = INF;
  51. for(int i = ; i < n; ++i) {
  52. scanf("%d%d%d", &a, &b, &c);
  53. a+=; b+=;
  54. add(a - , b, c);
  55. m = max(m, b);
  56. st = min(st, a - );
  57. }
  58. for(int i = ; i <= m; ++i) {
  59. add(i, i - , -);
  60. add(i - , i, );
  61. }
  62. SPFA(st, m);
  63. printf("%d\n", d[m]);
  64. }
  65. return ;
  66. }

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. net 页面跳转

    前台: < a href="xx.html" target="_blank"> 后台: Response.Redirect("XXX.as ...

  2. java课后作业

    课后作业之字串加密: 设计思想: 1.输入要加密的英文子串str 2.定义num=str的字符串长度 3.将字符串转化为单个字符 4.每个字符+3,向后移3个 5.定义str1,将新得到的每个字符加到 ...

  3. vs c# int & int32

    在vs c#中,int就等价于int32, 所以通常也是使用int32 当在统计总数时,最好使用int32,int16数值范围太小,如果超出,就会变成随机数.

  4. FILE文件操作

    http://www.jb51.net/article/37688.htm fopen(打开文件)相关函数 open,fclose表头文件 #include<stdio.h>定义函数 FI ...

  5. mybatis配置问题

    //当构造函数有多个参数时,可以使用constructor-arg标签的index属性,index属性的值从0开始. <bean id="sqlSession" class= ...

  6. 利用drozer进行Android渗透测试

    一.安装与启动 1. 安装 第一步:从 http://mwr.to/drozer 下载Drozer (Windows Installer) 第二步:在 Android 设备中安装 agent.apk ...

  7. 对Object类中方法的深入理解

    看一下API中关于Object的介绍: 类 Object 是类层次结构的根类.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个类的方法. 那么Object中到底有哪些方法,各自有什 ...

  8. sdut 2445 小学数学

    小学数学 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdutoj/p ...

  9. GMap.Net开发之在WinForm和WPF中使用GMap.Net地图插件

    GMap.NET是什么? 来看看它的官方说明:GMap.NET is great and Powerful, Free, cross platform, open source .NET contro ...

  10. CodeForces 371D Vessels(树状数组)

    树状数组,一个想法是当往p注水时,认为是其容量变小了,更新时二分枚举,注意一些优化. #include<cstdio> #include<iostream> #include& ...