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了好几次. 后来偷看数据才想到这题还有两个隐藏的约束条件. 这题前缀和 ...
随机推荐
- rsync错误日志
问题一: @ERROR: chroot failed rsync error: error starting client-server protocol (code 5) at main.c(152 ...
- 细谈WEB标准
最近有些朋友很是疑惑web标准是什么,我在这里先做一个总结,有更好的见解的可以私信给我! 首先切入正题之前先谈一下什么叫DOCTYPE,DOCTYPE是document type(文档类型)的简写,主 ...
- Android笔记:获取屏幕信息
像素密度(dpi) float xdpi = getResources().getDisplayMetrics().xdpi;float ydpi = getResources().getDispla ...
- nmake geos
参考:http://blog.sina.com.cn/s/blog_82a2a7d301010f87.html 1 打开visual studio command prompt 该工具位于 开始程序 ...
- Angular.JS
AngularJS是什么? 完全使用 JavaScript编写的客户端技术.同其他历史悠久的 Web技术( HTML. CSS 和JavaScript)配合使用,使Web应用开发比以往更简单.更快捷. ...
- Swift - @IBDesignable和@IBInspectable
前言: 用storyboard/xib搞项目时,一些属性在Interface Builder上时无法设置,比如常用的layer的一些属性cornerRadius,borderColor等 (有时没必须 ...
- 三、jQuery--Ajax基础--Ajax全接触--jQuery中的AJAX
用jQuery实现Ajax jQuery.ajax([settings]) type:类型,“POST”或“GET”,默认为“GET” url:发送请求的地址 data:是一个对象,连同请求发送到服务 ...
- CentOS出错You don't have permission to access on this server
之前配置phpmyadmin的时候,在浏览器上输入http://192.168.8.250/phpmyadmin/ 也遇到了You don't have permission to access on ...
- lsof -ntP -i:端口取出 动行程序的PID 然后xargs kill -9 这个进程
[root@ok ok]# lsof -ntP -i: [root@ok ok]# netstat -lnutp|grep tcp /dnsmasq tcp /sshd tcp ::: :::* LI ...
- C#的初始化器
using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...