POJ1201 Intervals差分约束系统(最短路)
Description
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
Output
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
Source
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 5e4 + ;
struct Edge {
int v, cost;
Edge() {}
Edge(int v, int cost) : v(v), cost(cost) {}
};
vector<Edge> E[N];
void add(int u, int v, int w) {
E[u].push_back(Edge(v, w));
}
bool vis[N];
int d[N];
void SPFA(int st, int n) {
memset(vis, false, sizeof vis);
for(int i = ; i <= n; ++i) d[i] = -INF;
d[st] = ;
vis[st] = true;
queue<int> que;
while(!que.empty()) que.pop();
que.push(st);
while(!que.empty()) {
int u = que.front();
que.pop();
vis[u] = false;
int sx = E[u].size();
for(int i = ; i < sx; ++i) {
int v = E[u][i].v;
int w = E[u][i].cost;
if(d[v] < d[u] + w) {
d[v] = d[u] + w;
if(!vis[v]) {
vis[v] = true;
que.push(v);
}
}
}
}
}
int main() {
int n;
while(~scanf("%d", &n)) {
int a, b, c, m = , st = INF;
for(int i = ; i < n; ++i) {
scanf("%d%d%d", &a, &b, &c);
a+=; b+=;
add(a - , b, c);
m = max(m, b);
st = min(st, a - );
}
for(int i = ; i <= m; ++i) {
add(i, i - , -);
add(i - , i, );
}
SPFA(st, m);
printf("%d\n", d[m]);
}
return ;
}
POJ1201 Intervals差分约束系统(最短路)的更多相关文章
- POJ1201 Intervals[差分约束系统]
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26028 Accepted: 9952 Descri ...
- [BZOJ2330][SCOI2011]糖果 差分约束系统+最短路
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2330 类似于题目中这种含有不等式关系,我们可以建立差分约束系统来跑最长路或最短路. 对于一 ...
- Intervals(差分约束系统)
http://poj.org/problem?id=1201 题意:给定n个整数闭区间[a,b]和n个整数c,求一个最小的整数集合Z,满足Z里边的数中范围在闭区间[a,b]的个数不小于c个. 思路:根 ...
- POJ 1201 Intervals (差分约束系统)
题意 在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点. 思路 差分约束求最小值.把不等式都转换为&g ...
- POJ1201 Intervals(差分约束)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 10966 Description You ...
- poj1201 Intervals——差分约束
题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...
- poj 1201/zoj 1508 intervals 差分约束系统
// 思路 : // 图建好后 剩下的就和上一篇的 火烧连营那题一样了 求得解都是一样的 // 所以稍微改了就过了 // 最下面还有更快的算法 速度是这个算法的2倍#include <ios ...
- PKU 1201 Intervals(差分约束系统+Spfa)
题目大意:原题链接 构造一个集合,这个集合内的数字满足所给的n个条件,每个条件都是指在区间[a,b]内至少有c个数在集合内.问集合最少包含多少个点.即求至少有多少个元素在区间[a,b]内. 解题思路: ...
- POJ1201 Intervals(差分约束系统)
与ZOJ2770一个建模方式,前缀和当作点. 对于每个区间[a,b]有这么个条件,Sa-Sb-1>=c,然后我就那样连边WA了好几次. 后来偷看数据才想到这题还有两个隐藏的约束条件. 这题前缀和 ...
随机推荐
- Yii里获取当前controller和action的id
Yii里获取当前controller和action的id 在控制器里$name = $this->getId(); // controller$name = $action->id; ...
- Hibernate简单分页
5.1 准备工作 建立项目,加入jar 建立hibernate.cfg.xml 建立pojo类和对应的映射文件 5.2 建立vo类PageEntity package org.guangsoft.vo ...
- 基于Spring的可扩展Schema进行开发自定义配置标签支持
一.背景 最近和朋友一起想开发一个类似alibaba dubbo的功能的工具,其中就用到了基于Spring的可扩展Schema进行开发自定义配置标签支持,通过上网查资料自己写了一个demo.今天在这里 ...
- 移动端hrml模板
<!DOCTYPE html><html><head> <title>时钟</title> <meta charset="u ...
- T4模板
T4,即4个T开头的英文字母组合:Text Template Transformation Toolkit. T4文本模板,即一种自定义规则的代码生成器.根据业务模型可生成任何形式的文本文件或供程序调 ...
- python基础——类和实例
python基础——类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都 ...
- redis配置详情
# Redis configuration file example # Note on units: when memory size is needed, it is possible to sp ...
- 20145206《Java程序设计》实验五Java网络编程及安全
20145206<Java程序设计>实验五 Java网络编程及安全 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 实验步骤 我和201451 ...
- 与你相遇好幸运,The Moe Node.js Code Style Guide
The Moe Node.js Code Style Guide By 一个最萌的开发者 @2016.9.21 >>代码是人来阅读的,格式规范的代码是对编程人员最好的礼物 :) > ...
- Vs 控件错位 右侧资源管理器文件夹点击也不管用,显示异常
问题:显卡驱动异常. 缘由:驱动精灵万能显卡安装系统 解决方案:根据笔记本型号去官网下载适配显卡驱动.