【POJ 1201】 Intervals(差分约束系统)

11

1716的升级版 把原本固定的边权改为不固定。

Intervals
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23817   Accepted: 9023

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

关于差分约束系统,百度各种大牛博客讲的都非常具体,简单说就是通过不等关系建立约束系统图。然后跑最短路(大于关系则跑最长路)

回到此题,题目要求找出一个最小集合S。满足对于n个范围[ai,bi],S中存在ci个及ci个以上不同的点在范围内

令Zi表示满足条件的情况下,0~i点至少有多少点在集合内

则Zbi-Zai >= ci

仅仅有这一个条件构造出来的图可能不是全然连通的,所以须要找一些“隐含条件”

不难发现 对于相邻的点 0 <= Zi-Z(i-1) <= 1 保证关系符同样 转化为

Zi-Z(i-1) >= 0

Z(i-1)-Zi >= -1

用这三个关系,就可以构造差分约束系统,然后SPFA或者Bellman跑一趟最长路(满足全部条件)

代码例如以下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue> using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 1e5;
const int mod = 1e9+7;
const double eps = 1e-8; struct Edge
{
int v,w,next;
}; Edge eg[233333];
int head[50050];
bool vis[50050];
int dis[50050];
int tp,st,en; void Add(int u,int v,int w)
{
eg[tp].v = v;
eg[tp].w = w;
eg[tp].next = head[u];
head[u] = tp++;
} int SPFA()
{
memset(vis,0,sizeof(vis));
memset(dis,-INF,sizeof(dis));
queue <int> q;
dis[st] = 0;
vis[st] = 1;
int u,v,w; q.push(st); while(!q.empty())
{
u = q.front();
q.pop();
vis[u] = 0;
for(int i = head[u]; i != -1; i = eg[i].next)
{
v = eg[i].v;
w = eg[i].w;
if(dis[v] < dis[u]+w)
{
dis[v] = dis[u]+w;
if(!vis[v])
{
q.push(v);
vis[v] = 1;
}
}
}
}
return dis[en];
} int main(int argc,char **argv)
{
int n;
int u,v,w; while(~scanf("%d",&n))
{
tp = 0;
memset(head,-1,sizeof(head)); en = 0,st = INF; while(n--)
{
scanf("%d%d%d",&u,&v,&w);
Add(u,v+1,w);
en = max(en,v+1);
st = min(st,u);
} for(int i = st; i < en; ++i)
{
Add(i,i+1,0);
Add(i+1,i,-1);
}
printf("%d\n",SPFA());
}
return 0;
}

最新文章

  1. javascript运行机制
  2. oracle 11g express 快速入门
  3. DIP依赖倒置原则
  4. CTO、技术总监、首席架构师的区别
  5. 【leetcode】Find Peak Element
  6. 利用Windows自带的Certutil查看文件MD5
  7. [Objective-c 基础 - 3.4] protocol
  8. 标签form表单里的属性简单介绍了一下
  9. tomcat 8.0 安装
  10. 【Javascript】搞定JS面试——跨域问题
  11. (@WhiteTaken)设计模式学习——代理模式
  12. lesson - 7 vim 详解
  13. iOS进阶之如何进行 HTTP Mock(转载)
  14. Vue的计算属性,监视属性代码理解
  15. day02格式化输出等
  16. Nginx 简单的cpu配置
  17. 13.BeanUtils组件-基础.md
  18. linux系统负载
  19. python笔记01:基础知识
  20. windows设置共享

热门文章

  1. synchronized关键字详解(一)
  2. 将npm修改为cnpm
  3. jQuery与js的区别,并有基本语法详解,
  4. JS——锚点的运用
  5. Python星号表达式
  6. 关于WEB开发下面DIV层被OCX控件拦住问题
  7. Windows下Unity安装
  8. jquery操作元素之间相邻的元素的获取方式
  9. 第十节:pandas之loc()、iloc()与ix()索引
  10. h5知识总结