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了好几次. 后来偷看数据才想到这题还有两个隐藏的约束条件. 这题前缀和 ...
随机推荐
- mongodb3.x用户角色
用户和角色是多对多的关系,一个用户可以对应多个角色,一个角色可以拥有多个用户.用户角色的不同对应的权限也是不一样的.下面是一些分配给用户的常见的角色. read ...
- 3DS MAX调慢摄像机动画
在3ds max的右下角找到时间配置,然后出现该对话框,然后调整结束时间,将原来的时间翻倍,就能够是摄像机动画变慢. 下图的旋转速度比上图慢一半.
- 多线程编程4 - NSOperationQueue
一.简介 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的.也可以将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步 ...
- ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(五) 之 加好友,加群流程,消息管理和即时消息提示的实现
前言 前前一篇留了个小问题,在上一篇中忘了写了,就是关于LayIM已经封装好的上传文件或者图片的问题.对接好接口之后,如果上传速度慢,界面就会出现假死情况,虽然文件正在上传.于是我就简单做了个图标替代 ...
- iOS真机调试
备注:本阶段之前的修改配置文件.准备脚本等,只需要做一次.但本阶段的操作,对每个需要真机调试的工程都要做一遍. ① 禁用Xcode自动的签名操作 将工程配置“Build Settings”中所有的Co ...
- Android下拉刷新效果实现
本文主要包括以下内容 自定义实现pulltorefreshView 使用google官方SwipeRefreshLayout 下拉刷新大致原理 判断当前是否在最上面而且是向下滑的,如果是的话,则加载数 ...
- struts2中一些常用的写法 记录
1.对日期进行处理 Date current = new Date(); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat ...
- iOS 判断第一个字符是数字还是汉字
NSString *titleStr = @"琳小兮"; //先截取字符串,拿到第一个字符 NSString *firstStr = [titleStr s ...
- openfire 服务器名称:后面的黄色叹号
然后点击重新获取证书,然后重新启动服务,问题解决!
- 重拾smslib
http://www.tuicool.com/articles/mm2yQrN http://blog.csdn.net/ll136078/article/details/8737348 http:/ ...