传送门: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. HihoCoder第六周:01背包问题

    01背包问题大二的时候就接触过了,几行关键代码自己也都看过很多遍了,但是很多代码一直都没能理解.所以今天拿表来好好地画一画,弄懂其中的动态规划究竟什么含义. 1038 : 01背包 时间限制:2000 ...

  2. sqlite if not exists应用实例

    INSERT or replace INTO [main].[RecordInfo]([WorkID],[bArtificialAttendance],[fThreshold],[Attendance ...

  3. WTL之VS2013环境搭建

    新版博客已经搭建好了,有问题请访问 htt://www.crazydebug.com 从国外回来,在老家入职了新公司,做c++开发,刚到新公司要熟悉公司的项目代码,目前公司在做一个主播聚合平台,界面采 ...

  4. 6 —— node —— 响应一个完整的页面

      const http = require('http'); const fs = require('fs'); const server = http.createServer(); server ...

  5. ubuntu12.04 eclipse安装PyDev

    在ubuntu软件中心安装的eclipse版本为3.7,install new software时,搜索出来的PyDev版本较高(5.6...): 高版本的PyDev要求较高版本的eclipse.详情 ...

  6. MacType

    #前言 这几天实在是嫌弃Win10垃圾的字体渲染效果--发虚模糊,索性从网上找了个系统字体渲染软件即MacType给系统字体改头换面. #使用效果 这里贴出两个场景的效果对比(单击图片查看具体效果) ...

  7. Windows + Python + flup + flask + fastcgi + Nginx配置

    Nginx配置 # HTTPS server { listen ssl; server_name kvaccount.xx.io; ssl_certificate "C:/xx/conf/s ...

  8. 学习spring第五天 mybatis+spring的整合(maven多模块数据查询使用了分页和连接池),以及aop

    mybatis+spring的整合: 导入的依赖:1.数据库连接:mysql-connector-java 2.连接池:druid 3.servlet:javax.servlet-api 4.jstl ...

  9. hex、Base64

    一.什么是Hex 将每一个字节表示的十六进制表示的内容,用字符串来显示. 二.作用 将不可见的,复杂的字节数组数据,转换为可显示的字符串数据 类似于Base64编码算法 区别:Base64将三个字节转 ...

  10. c++ 字符串转数字或数字转字符串

    在C++中字符串转换为数字,或数字转换为字符串,用到如下函数: _itoa atoi.atof.itoa.itow _itoa_s 1.整形转换为字符串: wchar_t * _itot(int _V ...