传送门:http://poj.org/problem?id=1201

题意:

有n个如下形式的条件:,表示在区间[, ]内至少要选择个整数点.问你满足以上所有条件,最少需要选多少个点?

思路:第一道差分约束题,有关差分约束知识详见https://blog.csdn.net/whereisherofrom/article/details/78922648(个人感觉这个博主写的挺好的)

表示从区间[0,x]中选择的整数点个数,则有=c_i\rightarrow s_{b_i+1}-s_{a_i}>=c_i" class="mathcode" src="https://private.codecogs.com/gif.latex?s_%7Bb_i%7D-s_%7Ba_i-1%7D%3E%3Dc_i%5Crightarrow%20s_%7Bb_i+1%7D-s_%7Ba_i%7D%3E%3Dc_i">(因为=0" class="mathcode" src="https://private.codecogs.com/gif.latex?a_i%3E%3D0">,会出现数组下标为-1的情况),如果只靠这个式子是无法得出答案的,题中还有隐藏的约束即是=0" class="mathcode" src="https://private.codecogs.com/gif.latex?s_%7Bi+1%7D-s_i%3E%3D0">和=-1" class="mathcode" src="https://private.codecogs.com/gif.latex?s_i-s_%7Bi+1%7D%3E%3D-1">,题中要求最小值,所以用spfa跑一遍最长路即可。

代码:

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn = 50005;
const int INF = 0x3f3f3f3f; int tot;
struct node
{
int to;
int next;
int w;
};
node edges[maxn * 3]; int head[maxn];
int d[maxn];
bool vis[maxn]; void add_edges(int u, int v, int w)
{
edges[++tot].to = v;
edges[tot].w = w;
edges[tot].next = head[u];
head[u] = tot;
} int l = 50005;
int r = -1; void spfa()
{
for(int i = l; i <= r; i++)
vis[i] = false, d[i] = -INF;
d[l] = 0;
queue<int>q;
q.push(l);
while(!q.empty())
{
int x = q.front();
q.pop();
vis[x] = false;
for(int i = head[x]; i; i = edges[i].next)
{
node v = edges[i];
if(d[v.to] < d[x] + v.w)
{
d[v.to] = d[x] + v.w;
if(!vis[v.to])
{
vis[v.to] = true;
q.push(v.to);
}
}
}
}
}
int main()
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add_edges(a, b + 1, c);
r = max(r, b + 1); //求区间右端点
l = min(l, a); //求区间左端点
}
for(int i = l; i < r; i++)
{
add_edges(i + 1, i, -1);
add_edges(i, i + 1, 0);
}
spfa();
cout << d[r] << endl;
}

POJ 1201 Intervals【差分约束】的更多相关文章

  1. poj 1201 Intervals(差分约束)

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

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

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

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

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

  4. POJ 2101 Intervals 差分约束

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27746   Accepted: 10687 Description You ...

  5. poj 1201 Intervals(差分约束)

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

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

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

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

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

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

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

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

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

随机推荐

  1. 操作数据库的SqlHelper类

    public static class SqlHelper { public static readonly string connstr = ConfigurationManager.Connect ...

  2. js ajax跨域调用

    正常使用ajax调用java.com $.ajax({ type: 'get', url: 'http://www.java.com/custinfo?id=888', dataType: 'json ...

  3. 逆向--C函数和汇编

    C函数和汇编 C代码 (编译工具gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609平台ubuntu i386 32位) int bar(int c ...

  4. Docker 镜像(image)

    版权所有,未经许可,禁止转载 章节 Docker 介绍 Docker 和虚拟机的区别 Docker 安装 Docker Hub Docker 镜像(image) Docker 容器(container ...

  5. H5页面跳转到小程序代码

    1.H5页面加代码直接打开小程序 <script type="text/javascript src="https://res.wx.qq.com/open/js/jweix ...

  6. P 1029 旧键盘

    转跳点:

  7. POJ 3071:Football

    Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3600   Accepted: 1844 Descript ...

  8. ContentProvider ContentResolver ContentObserver 内容:提供、访问、监听

    内容提供 public class PersonContentProvider extends ContentProvider{ private static final String AUTHORI ...

  9. Dubbo+Zookeeper 入门Demo

    1.Zookeeper安装及启动 可参考这篇文章https://www.cnblogs.com/geekdc/p/5948326.html 从下载到启动都描述的很详细,按照文章一步一步走即可. 2.D ...

  10. 转: 十大Intellij IDEA快捷键

    Intellij IDEA中有很多快捷键让人爱不释手,stackoverflow上也有一些有趣的讨论.每个人都有自己的最爱,想排出个理想的榜单还真是困难.以前也整理过Intellij的快捷键,这次就按 ...