poj 1201 TYVJ 1415 Intervals
Description:
给定n个闭区间[ai,bi] 和n个整数ci,你需要构造一个集合Z,使得对于任何的i∈[1,n],Z中满足x∈[ai,bi]的x不少于ci个 求这样的整数集合Z至少包含多少个数
思路:建立差分约束系统的模型s[k]表示0~k间选取多少个整数,根据题意有s[bi] - s[ai - 1] ≥ ci
不过还要增加一些隐含条件 s[k] - s[k - 1] ≥ 0,s[k] - s[k -1] ≤ 1
因此,将输入最大的数bi作为图中的节点,从每个k - 1到k连长度为0的有向边,k到k - 1连长度为 -1 的有向边。从每个到ai - 1 到 bi 连长度为ci的有向边
起点为0,终点为max(bi)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int N = , M = 1e6 + ;
typedef pair<int,int> P; int head[N], now;
struct edges{
int to, next, w;
}edge[M<<];
void add(int u, int v, int w){ edge[++now] = {v, head[u], w}; head[u] = now;} int n, d[N], s;
bool vis[N];
queue<int> q;
void spfa(){
d[s] = ;
q.push(s);
vis[s] = ;
while(!q.empty()){
int x = q.front(); q.pop();
vis[x] = ;
for(int i = head[x]; i; i = edge[i].next){
int v = edge[i].to;
if(d[v] < d[x] + edge[i].w || (d[v] == d[x] + edge[i].w && v == x + )){
d[v] = d[x] + edge[i].w;
if(!vis[v]) q.push(v), vis[v] = ;
}
}
}
}
int main(){
scanf("%d", &n);
int x, y, z;
int mx = ;
for(int i = ; i <= n; i++){
scanf("%d%d%d", &x, &y, &z);
x++, y++; // 全都向上加1,防止数组向下溢出
mx = max(mx, y);
add(x - , y, z);
}
for(int i = ; i <= mx; i++)
add(i - , i, ), add(i, i - , -);
s = ;
spfa();
printf("%d\n",d[mx]);
return ;
}
poj 1201 TYVJ 1415 Intervals的更多相关文章
- poj 1201/zoj 1508 intervals 差分约束系统
// 思路 : // 图建好后 剩下的就和上一篇的 火烧连营那题一样了 求得解都是一样的 // 所以稍微改了就过了 // 最下面还有更快的算法 速度是这个算法的2倍#include <ios ...
- poj 1201 Intervals 解题报告
Intervals Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Submit Statu ...
- 【POJ 1201】 Intervals(差分约束系统)
[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS Memory Limit: ...
- POJ 1201 && HDU 1384 Intervals(差动制动系统)
职务地址:POJ 1201 HDU 1384 依据题目意思.能够列出不等式例如以下: Sj-Si>=c; Si-S(i-1)>=0; S(i-1)-Si>=-1; 然后用最短路s ...
- POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束
POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...
- 【POJ 1716】Integer Intervals(差分约束系统)
id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS Memory L ...
- POJ 1201 差分方程分析
POJ 1201 给你N个闭区间.每个区间分别为[ai,bi],你必须在这个区间上至少取ci个不同的整数. 现要求所有区间满足各自的条件. 问最少需要选多少个点. 例如[3,7](3) [8,10] ...
- POJ 3225 Help with Intervals(线段树)
POJ 3225 Help with Intervals 题目链接 集合数字有的为1,没有为0,那么几种操作相应就是置为0或置为1或者翻转,这个随便推推就能够了,然后开闭区间的处理方式就是把区间扩大成 ...
- POJ 1201 & HDU1384 & ZOJ 1508 Intervals(差分约束+spfa 求最长路径)
题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...
随机推荐
- ruby net/http模块使用
ruby中的NET::HTTP:这里暂时先列出几个固定用法: 其中一,二不支持请求头设置(header取ruby默认值),只能用于基本的请求,不支持持久连接,如果您执行许多HTTP请求,则不推荐它们: ...
- Infinite Maze CodeForces - 196B
We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A ...
- R语言学习笔记(十一):零碎知识点(26-30)
26--aggregate( ) 函数aggregate()对分组中的每一个变量调用tapply()函数. aggregate(a,list,f) 第二个参数必须是列表.也就是因子部分. 第三个参数即 ...
- ccpc 2018 final G - Pastoral Life in Stardew Valley
#include <iostream> #include<cstdio> #include<cstring> #include<queue> using ...
- 【Java】关于Spring MVC框架的总结
SpringMVC是一种基于Java,实现了Web MVC设计模式,请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将Web层进行职责解耦.基于请求驱动指的就是使用请求-响应模型,框架的 ...
- kafka topic 完全删除
kafka topic 完全删除 1.自动删除脚本(得配置server.properties 中 delete.topic.enable=true) ./kafka-topics.sh --zoo ...
- 20145202 《Java程序设计》实验五实验报告
一.实验内容 1.用书上的TCP代码,实现服务器与客户端. 2.客户端与服务器连接 3.客户端中输入明文,利用DES算法加密,DES的秘钥用RSA公钥密码中服务器的公钥加密,计算明文的Hash函数值, ...
- android去掉button默认的点击阴影
查了资料,发现别人都是说加一个style属性. style="?android:attr/borderlessButtonStyle" 加上了确实管用,但是我绝不是不求甚解的人.追 ...
- Gradle 设置本地meaven
repositories { maven { url uri("F:\\meaven")} }
- linux安装oracle远程客户端
文章参考:http://blog.csdn.net/caomiao2006/article/details/11901123 感谢博友分享O(∩_∩)O~ 安装oracle 远程客户端(一般情况下本地 ...